Javascript send "this"对象作为参数

Javascript send "this" object as parameter

本文关键字:quot 参数 对象 Javascript this send      更新时间:2023-09-26

我想知道是否有可能将对象作为参数传递给javascript中的另一个"类"。

例如

function parent = function() {
    var child = new child();
    this.child.setParent(this);
}

我知道这个例子是多余的,但我希望它能让你明白我的意思。

是的。你的例子是正确的。

附加阅读:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function

https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope

https://developer.mozilla.org/en/JavaScript/Reference/Statements/function

当然,为什么不呢?它是一个值,就像其他值一样。

也就是说,this在JavaScript中偶尔会有奇怪的语义,您需要确保它与您认为的绑定,特别是在处理闭包时。