Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:如何设置';这';对象方法_Javascript - Fatal编程技术网

Javascript:如何设置';这';对象方法

Javascript:如何设置';这';对象方法,javascript,Javascript,我如何才能在这样一个函数中的对象中访问此: function Foo (name) { this.name = name; var _ = {}; _.bar = {}; _.bar.print() = function () { return this.name; // here I have access to the wrong this }; this.print = function () {

我如何才能在这样一个函数中的对象中访问此:

function Foo (name) {
    this.name = name;

    var _ = {};
    _.bar = {};
    _.bar.print() = function () {
        return this.name;            // here I have access to the wrong this
    };

    this.print = function () {
        console.log(_.bar.print());
    };
}

只需将required
保存到一个变量中,并在其他范围内使用它即可

function Foo (name) {
    var _this = this; // <-- store function's this
    this.name = name;

    var _ = {};
    _.bar = {};
    _.bar.print() = function () {
        return _this.name;            // use stored _this
    };

    this.print = function () {
        console.log(_.bar.print());
    };
}
函数Foo(名称){

var _this=this;//只需将必需的
this
保存到一个变量中,并在其他范围内使用它

function Foo (name) {
    var _this = this; // <-- store function's this
    this.name = name;

    var _ = {};
    _.bar = {};
    _.bar.print() = function () {
        return _this.name;            // use stored _this
    };

    this.print = function () {
        console.log(_.bar.print());
    };
}
函数Foo(名称){

var _this=this;//只需将必需的
this
保存到一个变量中,并在其他范围内使用它

function Foo (name) {
    var _this = this; // <-- store function's this
    this.name = name;

    var _ = {};
    _.bar = {};
    _.bar.print() = function () {
        return _this.name;            // use stored _this
    };

    this.print = function () {
        console.log(_.bar.print());
    };
}
函数Foo(名称){

var _this=this;//只需将必需的
this
保存到一个变量中,并在其他范围内使用它

function Foo (name) {
    var _this = this; // <-- store function's this
    this.name = name;

    var _ = {};
    _.bar = {};
    _.bar.print() = function () {
        return _this.name;            // use stored _this
    };

    this.print = function () {
        console.log(_.bar.print());
    };
}
函数Foo(名称){
var\u this=this;//您可以这样做

_.bar.print() = (function () {
    return this.name;           
}).bind(this);
但整个过程看起来毫无意义地复杂。

你可以这样做

_.bar.print() = (function () {
    return this.name;           
}).bind(this);
但整个过程看起来毫无意义地复杂。

你可以这样做

_.bar.print() = (function () {
    return this.name;           
}).bind(this);
但整个过程看起来毫无意义地复杂。

你可以这样做

_.bar.print() = (function () {
    return this.name;           
}).bind(this);

但是整个过程看起来毫无意义地复杂。

这里有一个语法错误,但是你可能想要
\uu.bar.print=(函数(){…}).bind(这个)
?这里有一个语法错误,但是你可能想要
\ucode>\ubar.print=(函数(){…})。bind(这个)
?这里有一个语法错误,但是你可能想要
\ucode>.bar.print=(函数(){…}).bind(this)
?此处有语法错误,但您可能需要
\uuu.bar.print=(function(){…}).bind(this)
?谢谢,这就是我想要的。是的,我知道它看起来很复杂,但1500行代码看起来都很复杂。我的意思是你可能使用了错误的模块模式。谢谢,这就是我想要的。是的,我知道它看起来很复杂,但1500行代码看起来都很复杂。我的意思是你可能使用了错误的模块模式。Thanks,这就是我想要的。是的,我知道它看起来很复杂,但1500行代码看起来都很复杂。我的意思是你可能使用了错误的模块模式。谢谢,这就是我想要的。是的,我知道它看起来很复杂,但1500行代码看起来都很复杂。我的意思是你可能使用了错误的模块模式。