简体   繁体   English

当方法传递给另一个函数时,如何在实例方法中访问实例属性?

[英]How to access instance property in an instance method when the method is being passed into an another function?

I know in the code below it will print out undefined if I click on the button, because this.field becomes within the context of the button and not Container . 我知道在下面的代码中,如果单击按钮,它将打印出undefined ,因为this.field变为按钮的上下文而不是Container My question is how can I access this.field of Container when this.func is passed into another function, which is a different context scope than Container . 我的问题是如何访问this.fieldContainerthis.func传递到另一个函数,这比一个不同的上下文范围Container

function Container(){
    this.field = 'field';

    $('button').click(this.func);
}

Container.prototype.func = function(){
   console.log(this.field);
}

I know I can do this, but is there a better way? 我知道我可以做到这一点,但有更好的方法吗? Because I'd rather define the methods outside the constructor so I won't clutter it. 因为我宁愿在构造函数之外定义方法,所以我不会混淆它。

function Container(){
    var thisObj = this;
    this.field = 'field';

    $('button').click(function(){ console.log(thisObj.field) });
}

How about passing an anonymous function? 传递一个匿名函数怎么样?

function Container(){
    this.field = 'field';
    var self = this;

    $('button').click(function() {
        self.func()
    });
}

You have not many choices here... 你在这里没有多少选择......

jQuery makes it a bit easier for you and offers $.proxy : jQuery让你更容易,并提供$.proxy

 $('button').click($.proxy(this.func, this));
 // or
 $('button').click($.proxy(this, 'func'));

Pass the object reference in as the event data: 将对象引用作为事件数据传递:

function Container(){
    this.field = 'field';

    $('button').click(this, this.func);
}

Container.prototype.func = function(e){
   console.log(e.data.field);
}

See here: http://jsfiddle.net/gilly3/nwtqJ/ 见这里: http//jsfiddle.net/gilly3/nwtqJ/

I'd just like to point out that, generally speaking, you should avoid thinking of the 'this' variable as having any resemblance to the one in java. 我只想指出,一般来说,你应该避免将'this'变量视为与java中的变量有任何相似之处。 You clearly understand that, but it always bears repeating. 你清楚地了解这一点,但它总是重复。 Since javascript's this always points to the object on which you called the function, your passing of an instance function to another object to use as its onclick leads to confusion. 由于javascript总是指向您调用该函数的对象,因此将实例函数传递给另一个对象以用作其onclick会导致混淆。 I don't know what you intend to do in the instance method itself, but there is no reason why an onclick handler should be an instance method of another object. 我不知道你打算在实例方法本身做什么,但是没有理由为什么onclick处理程序应该是另一个对象的实例方法。 If it's access to the members you want, they're public, so no need to have it be an instance method. 如果它可以访问您想要的成员,那么它们是公共的,因此不需要将它作为实例方法。 If the work to be done relates exclusively to the Container object, then the onclick should possibly be a separate function that would have a refernce to the container and call the instance method. 如果要完成的工作仅与Container对象相关,则onclick应该是一个单独的函数,它将引用容器并调用实例方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从传递给实例调用的“ bind”方法的预定义函数中访问对象实例的上下文? - How can I access an object instance's context from within a predefined function passed to the “bind” method invoked on the instance? 覆盖函数对象或访问实例方法中的方法,该方法传递给angularjs中的指令 - override a method in function object or access instance methods which is passed to a directive in angularjs 渲染前如何在实例上定义属性或方法 - How to define property or method on the instance before render 获取对实例方法内部的实例属性的访问 - Get access to instance attributes inside of instance method 如何将“类实例”或“this”推入“函数”方法 - How to push in 'class instance' or 'this' into a 'Function' method JS:传递一个包含函数/方法属性的 Object 然后在另一个文件中传递并调用传入的 Object 函数/方法属性? - JS: Pass an Object containing a Function/Method Property then Passing and Invoking the Passed in Object Function/Method Property in Another File? VueJS:'没有在实例上定义属性或方法' - 即使方法是在实例上定义的 - VueJS: 'Property or method is not defined on the instance' - even though method is defined on instance 为什么在类方法中使用我的实例属性时未定义? - Why is my instance property undefined when used in a class method? 将函数添加到构造函数原型的方法,该方法可以从构造函数实例访问此方法 - Add function to constructor prototype with method with access to this from constructor instance 在原型方法内访问匿名 function 内的实例变量 - Access an instance variable inside an anonymous function inside prototype method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM