node.js/javascript——嵌套回调

node.js/javascript——嵌套回调,javascript,node.js,Javascript,Node.js,别客气,我对javascript和这里的帖子还不熟悉 我的问题是为什么“this.b”在最初的MyClass实例化中没有更改?我在这里读到javascript没有块作用域(只有函数作用域)。如果这就是原因,那么为什么它不将“this.b”设置为“now me b”而将其视为“undefined”呢 谢谢 传递给该方法的匿名回调函数创建了一个新的作用域,因此当您在回调中说this.b时,它不再是相同的b,而是一个新的作用域 解决此问题的一种常见方法是在回调之外创建对要访问的此的引用: functi

别客气,我对javascript和这里的帖子还不熟悉

我的问题是为什么“this.b”在最初的MyClass实例化中没有更改?我在这里读到javascript没有块作用域(只有函数作用域)。如果这就是原因,那么为什么它不将“this.b”设置为“now me b”而将其视为“undefined”呢


谢谢

传递给该方法的匿名回调函数创建了一个新的作用域,因此当您在回调中说
this.b
时,它不再是相同的
b
,而是一个新的作用域

解决此问题的一种常见方法是在回调之外创建对要访问的
的引用:

function MyClass() {
    this.a = "me a";
    this.b = "me b";
};

MyClass.prototype.changeB = function(callback) {
    this.a = "now me A";
    doAnotherFunction(function(err, data) {
        this.b = "now me B";
    callback(null, this.b);});
};

function doAnotherFunction(callback) {
    callback(null, null);
};

main();

function main() {
    var myclass = new MyClass();
    myclass.changeB(function(err, data) {
    console.log("B: " + myclass.b + ", data: " + data);});
    console.log(JSON.stringify(myclass));
}


When this runs:
B: me b, data: now me B
{"a":"now me A","b":"me b"}

然后您可以在回调中引用self.b

传递给方法的匿名回调函数创建了一个新的作用域,因此当您在回调中说this.b
时,它不再是相同的
b
,而是一个新的作用域

解决此问题的一种常见方法是在回调之外创建对要访问的
的引用:

function MyClass() {
    this.a = "me a";
    this.b = "me b";
};

MyClass.prototype.changeB = function(callback) {
    this.a = "now me A";
    doAnotherFunction(function(err, data) {
        this.b = "now me B";
    callback(null, this.b);});
};

function doAnotherFunction(callback) {
    callback(null, null);
};

main();

function main() {
    var myclass = new MyClass();
    myclass.changeB(function(err, data) {
    console.log("B: " + myclass.b + ", data: " + data);});
    console.log(JSON.stringify(myclass));
}


When this runs:
B: me b, data: now me B
{"a":"now me A","b":"me b"}
然后您可以在回调中引用self.b

开始

对于您的特定示例,您可以这样编码,保存对
MyClass
实例的
this
的引用:

var self = this;
针对建议使用的示例:

MyClass.prototype.changeB = function(callback) {
    var me = this;
    this.a = "now me A";
    doAnotherFunction(function(err, data) {
        me.b = "now me B";
        callback(null, me.b);
    });
};
编辑:要了解示例中的
这是什么,请尝试添加一些日志记录:

MyClass.prototype.changeB = function(callback) {
    this.a = "now me A";
    var fn = function(err, data) {
        this.b = "now me B";
        callback(null, this.b);
    });
    // the first parameter to bind will be the 'this'
    // visible from inside the function
    doAnotherFunction(fn.bind(this));
};
首先

对于您的特定示例,您可以这样编码,保存对
MyClass
实例的
this
的引用:

var self = this;
针对建议使用的示例:

MyClass.prototype.changeB = function(callback) {
    var me = this;
    this.a = "now me A";
    doAnotherFunction(function(err, data) {
        me.b = "now me B";
        callback(null, me.b);
    });
};
编辑:要了解示例中的
这是什么,请尝试添加一些日志记录:

MyClass.prototype.changeB = function(callback) {
    this.a = "now me A";
    var fn = function(err, data) {
        this.b = "now me B";
        callback(null, this.b);
    });
    // the first parameter to bind will be the 'this'
    // visible from inside the function
    doAnotherFunction(fn.bind(this));
};

this.b=“now me b”
位于另一个函数中,可以在不同的执行上下文中调用。因此,在本例中,
this
并不意味着
MyClass
的实例。
this.b=“now me b”
在另一个函数中,并且会在不同的执行上下文中调用。因此,在本例中,
这个
并不意味着
MyClass
的实例。我认为这也有助于解释原因。
bind
将是另一个选项。感谢您的链接。我应该在我原来的帖子中补充说,我确实解决了这个问题,按照你的建议,将一个本地“me”变量设置为“this”(在多次拉毛之后:)。我关心的是,为什么嵌套函数没有将伪“this.b”标记为“undefined”?它只是自己实例化了一个新的MyClass对象吗?嵌套函数没有将伪
this.b
标记为未定义,因为您正在给它赋值,因此在
this
对象上创建了
b
属性。这个
对象在那一点上实际是什么,可以通过打印出来来确定,也可能是它的
类型。例如,
console.log(this,typeof this)
。我想在Node中这是模块的“全局”范围,但我不使用Node,所以我不确定。啊,我明白了-谢谢。但这种语言的“设计”实在不合逻辑。“doAnotherFunction”已经在显式创建的“this”中,但它创建了自己的“this”版本。这让你想知道在运行时有多少其他对象是在我们不知道的情况下随意创建的。我想这也会有助于解释原因。
bind
将是另一种选择。感谢链接Paul。我应该在我原来的帖子中补充说,我确实解决了这个问题,按照你的建议,将一个本地“me”变量设置为“this”(在多次拉毛之后:)。我关心的是,为什么嵌套函数没有将伪“this.b”标记为“undefined”?它只是自己实例化了一个新的MyClass对象吗?嵌套函数没有将伪
this.b
标记为未定义,因为您正在给它赋值,因此在
this
对象上创建了
b
属性。这个
对象在那一点上实际是什么,可以通过打印出来来确定,也可能是它的类型。例如,
console.log(this,typeof this)
。我想在Node中这是模块的“全局”范围,但我不使用Node,所以我不确定。啊,我明白了-谢谢。但这种语言的“设计”实在不合逻辑。“doAnotherFunction”已经在显式创建的“this”中,但它创建了自己的“this”版本。这让你想知道在运行时有多少其他对象是在我们不知道的情况下被随意创建的。谢谢Nils,我想我不能把你和Paul的答案都标记为正确。就像我在下面评论的那样,我认为用每个函数调用创建一个新对象是疯狂的,我不知道后面的推理。那么,this应该是什么呢?回调在MyClass的'changeB'方法中,因此回调的'this'应该是'changeB'的'this',它应该是'MyClass'(小写)的'this',在'main'方法中显式实例化了一次,并且只实例化了一次。一个人会想,这根本不应该有两个。谢谢尼尔斯,我想我不能把你和保罗的答案都标为正确。就像我在下面评论的那样,我认为用每个函数调用创建一个新对象是疯狂的,我不知道后面的推理。那么,this应该是什么呢?回调在MyClass的'changeB'方法中,因此回调的'this'应该是'changeB'的'this',它应该是'MyClass'(小写)的'this',在'main'方法中显式实例化了一次,并且只实例化了一次。不应该有两个'这是一点,一个会认为。