小编cpc*_*len的帖子

为什么``foo".bar = 42;`在ES6中以严格模式抛出`TypeError`?

根据ES5.1规范,程序"use strict;" "foo".bar = 42;会导致String创建一个对象,分配给它上面的属性,然后抛弃该对象,导致没有可观察到的影响 - 包括任何异常.(可以通过在与Opera 12中的ES5兼容的JS实现中尝试来确认效果的缺失.)

在现代JS实现中,它抛出一个TypeError代替尝试它:

"use strict"; "foo".bar = 42;
Run Code Online (Sandbox Code Playgroud)

我很确定新的行为是由ES6规范强制执行的,但是尽管多次阅读相关部分,但我看不出它被指定的位置TypeError.事实上,关键部分似乎没有变化:

6.2.3.2 PutValue(V,W)#

  1. ReturnIfAbrupt(V).
  2. ReturnIfAbrupt(W).
  3. 如果Type(V)不是Reference,则抛出ReferenceError异常.
  4. 让base为GetBase(V).
  5. 如果IsUnresolvableReference(V)为真,那么
    • ...
  6. 否则,如果IsPropertyReference(V)为真,那么
    • 一个.如果HasPrimitiveBase(V)为真,那么
      • 一世.断言:在这种情况下,base永远不会为null或未定义.
      • II.将base设置为ToObject(base).
    • 湾 我们成功了吗?base.[[Set]](GetReferencedName(V),W,GetThisValue(V)).
    • C.ReturnIfAbrupt(成功). …

javascript primitive typeerror ecmascript-5 ecmascript-6

25
推荐指数
2
解决办法
1296
查看次数

如何在 Go 中创建一流的地图迭代器?

我正在编写一个迭代映射中的条目的函数。我希望能够干净地处理迭代时从地图中添加或删除的项目,就像for k, v := range myMap { //...所做的那样,但我每次迭代只处理一个键/值对,所以我不能使用范围。我想要这样的东西:

func processItem(i iterator) bool {
     k, v, ok := i.next()
     if(!ok) {
         return false
     }
     process(v)
     return true
}

var m = make(map[string]widget)
// ...
i := makeIterator(m)
for processItem(i) {
    // code which might add/remove item from m here
}
Run Code Online (Sandbox Code Playgroud)

我知道 range 使用 ' hiter' 结构体和关联函数(如 中定义src/runtime/hashmap.go)来执行迭代。有没有什么方法可以访问这个迭代器作为一个具体化的(一流的)Go 对象?

是否有另一种迭代映射的策略可以很好地处理插入/删除,但提供一流的迭代器对象?

额外问题:是否有另一种迭代映射的策略,该策略还可以处理映射和迭代器被序列化到磁盘然后恢复的情况,迭代从停止的地方继续?(显然内置迭代range器不具备这个能力!)

dictionary iterator go

6
推荐指数
2
解决办法
1650
查看次数

有没有办法让json.Unmarshal()根据"type"属性选择结构类型?

我有一些形式的JSON:

[{
    "type": "car",
    "color": "red",
    "hp": 85,
    "doors": 4
}, {
    "type": "plane",
    "color": "blue",
    "engines": 3
}]
Run Code Online (Sandbox Code Playgroud)

我有类型car和plane满足车辆接口; 我想能够写:

var v []vehicle
e := json.Unmarshal(myJSON, &v)
Run Code Online (Sandbox Code Playgroud)

...让JSON用汽车和飞机填满我的车辆; 相反(并且不出所料)我只是得到"不能将对象解组为类型为main.vehicle的Go值".

供参考,以下是所涉及类型的合适定义:

type vehicle interface {
    vehicle()
}

type car struct {
    Type  string
    Color string
    HP    int
    Doors int
}

func (car) vehicle() { return }

type plane struct {
    Type    string
    Color   string
    Engines int
}

func (plane) vehicle() { return }

var _ vehicle …
Run Code Online (Sandbox Code Playgroud)

json interface go

4
推荐指数
1
解决办法
4128
查看次数

是否存在 IsCallable 为 false 但 IsConstructor 为 true 的任何 JS 对象?

ECMAScript 规范函数IsCallable返回真,如果它的参数有一个 [[Call]] 内部方法。它在规范中的多个地方使用,例如在 的定义中 Array.prototype.toString。

有一个类似的规范函数IsConstructor,如果它的参数有一个 [[Construct]] 内部方法,它返回真。

一些 JS 对象,包括大多数内置函数,例如escape可调用但不可构造的。 是否有任何可构造但不可调用的?

请注意,用户定义的和内置的类TypeError在作为普通函数调用时都会抛出,但仍然可以根据 IsCallable 的定义调用,这可以通过查看是否Array.prototype.toString会尝试将它们用作 的实现来确定join:

// {} is not callable, so toString falls back to Object.prototype.toString:
console.log('null:', Array.prototype.toString.apply({join: {}}));
Run Code Online (Sandbox Code Playgroud)

// WeakMap is callable (but throws TypeError):
console.log('null:', Array.prototype.toString.apply({join: WeakMap}));
Run Code Online (Sandbox Code Playgroud)

// User-defined classes also callable:
console.log('null:', Array.prototype.toString.apply({join: class Foo {}}));
Run Code Online (Sandbox Code Playgroud)

javascript constructor function language-lawyer

4
推荐指数
1
解决办法
478
查看次数

如何在测试时允许一个包访问另一个包的未导出数据?

在旅途中编程语言,第11.2.4节,存在的一个外部测试接入功能的一个例子fmt.isSpace()通过的声明IsSpace中fmt的export_test.go文件.这似乎是完美的解决方案,所以这就是我所做的:

a/a.go:

package a

var x int

func Set(v int) {
    x = v
}
Run Code Online (Sandbox Code Playgroud)

a/a_test.go:

package a
import "testing"

func TestSet(t *testing.T) {
    Set(42)
    if x != 42 {
        t.Errorf("x == %d (expected 42)", x)
    }
}

func Get() int {
    return x
}
Run Code Online (Sandbox Code Playgroud)

(运行go test中a/工作正常.)

b/b.go:

package b
import "path/to/a"

func CallSet() {
    a.Set(105)
}
Run Code Online (Sandbox Code Playgroud)

b/b_test.go

package b
import (
    "testing" …
Run Code Online (Sandbox Code Playgroud)

testing integration-testing go

0
推荐指数
1
解决办法
206
查看次数