Javascript 如何访问';这';原型函数中的变量(如果被重写)?

Javascript 如何访问';这';原型函数中的变量(如果被重写)?,javascript,jquery,Javascript,Jquery,如果我以这种方式调用构造函数中的set函数,this变量将被重写(就像我会以某种方式使用this.set.bind(对象、索引、元素)) Set是一个原型函数: Class.prototype.set = function(item,val){ //here I would like to get the real this in order to set data this.data[item] = val; } 有没有办法做到这一点 我已经这样做了,但我想知道第一个是否存在

如果我以这种方式调用构造函数中的set函数,
this
变量将被重写(就像我会以某种方式使用
this.set.bind(对象、索引、元素)

Set是一个原型函数:

Class.prototype.set = function(item,val){
    //here I would like to get the real this in order to set data
    this.data[item] = val;
}
有没有办法做到这一点

我已经这样做了,但我想知道第一个是否存在任何技巧

var o = $.parseJSON($selector.val();
for(var i in o)
   this.set(i,o[i]); //this works
更新日期: 这些措施也有效:

$.each($.parseJSON($selector.val()),(function(i,e){this.set(i,e);}).bind(this));


可以使用
Function.call()
Function.apply()
覆盖此

要保持原始状态,您需要以本机方式或使用一些polyfill来使用
Function.bind()

试试这个:

$.each($.parseJSON($selector.val()),this.set.bind(this));
更新:这种方法怎么样

var that = this;
$.each($.parseJSON($selector.val()), function() { return that.set.apply( that, arguments ); } );
典型的bind()的工作原理与此类似:

function myBind(func,ctx) {
    return function() {
        return func.apply(ctx,arguments);
    };
}
这也可以用来简化上面的第二个示例:

$.each($.parseJSON($selector.val()),myBind(this.set, this));

这种方法也不起作用,我认为jQuery忽略了绑定值
function myBind(func,ctx) {
    return function() {
        return func.apply(ctx,arguments);
    };
}
$.each($.parseJSON($selector.val()),myBind(this.set, this));