小编int*_*hab的帖子

如何在es6类中执行`var self = this`?

我在nodejs中运行以下代码

this.x = 'global x';
class Point {
    constructor(x) {
        this.x = x;
    }
    toString() {
        return this.x;
    }
}
var obj = new Point(1);
obj.toString();// 1 as expected
var a = obj.toString;// Here I can do something like var a = obj.toString.bind(obj); to get rid of the situation. But I am curious to know how can we write `var self = this`;
a();// TypeError: Cannot read property 'x' of undefined
Run Code Online (Sandbox Code Playgroud)

a();抛出错误.
我们怎样才能像var self = this;过去那样做es5 …

javascript ecmascript-6

32
推荐指数
2
解决办法
1万
查看次数

为什么在node.js v4.0.0中不推荐使用许多util.is*函数?

随着节点v4.0.0的发布.这个节点的版本弃用许多功能,如util.isArray,util.isRegEx,UTIL.isDate,util.isBoolean等等.

我想知道为什么这发生在节点上?

在ES6中是否有这些东西的原生支持?

或者节点是否提供了更好的解决方案而不是这些东西?

javascript node.js

21
推荐指数
1
解决办法
4775
查看次数

标签 统计

javascript ×2

ecmascript-6 ×1

node.js ×1