简体   繁体   English

如何将实例函数传递给外部函数?

[英]how do you pass an instance function to an outside function?

let's say I have I have the following in a file called file1.js: 假设我在名为file1.js的文件中包含以下内容:

//constructor
function Blah(){

    this.string = "hello there agent ";

}

//'method'
Blah.prototype.greeting = function(num){
    return  this.string + num;
}

Then in a file called file2.js I then have this: 然后在一个名为file2.js的文件中,我有以下内容:

function combine(num,funct){
    return funct(num);
}

And then finally, in an html file called file3, I have this: 最后,在一个名为file3的html文件中,我有以下内容:

var bond = new Blah();
document.write(combine(007,bond.greeting));

I am actually getting into the "greeting" method, but for some reason, the return value, instead of being a string, winds up not being NaN. 我实际上正在进入“ greeting”方法,但是由于某种原因,返回值不是字符串,而是不是NaN。 Any idea why? 知道为什么吗? the greeting() method seems to be ran at the proper time. greeting()方法似乎在适当的时间运行。 However, despite that, 007 seems to be getting interpreted as NaN anyway. 但是,尽管如此,无论如何,007似乎还是被解释为NaN。 Again, any suggestions as to what could be causing this? 同样,关于什么可能导致这种情况的任何建议?

Thanks a bunch in advance 提前谢谢一堆

First, depending on how you call the greeting method, the this value will be different. 首先,取决于您如何调用greeting方法, this值将有所不同。 If you call it like bond.greeting(num) then this will be bond . 如果你这样称呼它bond.greeting(num)那么this将是bond If you call it like funct(num) , where funct is bond.greeting , then this will be the global object. 如果您像funct(num)那样调用它,其中functbond.greeting ,则this将是全局对象。 You need to bind this permanently when passing the function along, to maintain its value no matter how you call the function. 您需要绑定this永久沿着传递功能时,要无论你如何调用该函数保持其价值。

Second, 007 === 7 . 其次, 007 === 7 If you want to print out 007 literally, then you should use a string: 如果要从字面上打印出007 ,则应使用一个字符串:

combine('007', bond.greeting.bind(bond));

Remember, this depends on how the function gets called, it is dynamic, and resolved an runtime, unless you bind it previously, like we did above. 记住, this取决于函数的调用方式,它是动态的以及如何解析运行时的,除非您像前面那样绑定了它,否则就不行了。

You're experiencing the special characteristics of the this keyword. 您正在体验this关键字的特殊特征。

Basically, this resolves to whatever you call a function from. 基本上, this可以解决您从中调用函数的任何问题。 In your case, you're calling it from the global scope from through func() , which makes this == window . 在您的情况下,您是通过func()从全局范围调用它的,这使this == window (Calling it through bond.greeting() makes this == bond .) (通过bond.greeting()进行调用使this == bond 。)

To resolve, this either bind the function or force the resolution: 要解决此问题,可以bind函数或强制执行分辨率:

// note that this method requires a shim for IE 8 and older
document.write(combine(007,bond.greeting.bind(bond)));

or 要么

function combine(num, obj, funct){
    // since funct is being called from obj, `this` == obj within the function
    return obj[funct](num);
}

document.write(combine(007,bond, 'greeting'));    

1) NaN is "Not a number" error. 1)NaN为“非数字”错误。 Try encapsulating 007 in quotes 2) Do you need file2.js or could you do without it? 尝试将007封装在引号中2)您是否需要file2.js还是没有它?

var bond = new Blah();
document.write(bond.greeting("007"));

The problem you have is when you pass the function as argument, it is passed by value, and then you loose the reference to the object who has the element string = "hello there agent "; 您遇到的问题是,当您将函数作为参数传递时,它通过值传递,然后您将对元素string = "hello there agent ";的对象的引用放开string = "hello there agent "; and when the function is executed, it executes "this.string" which doesn't exist inside the function, it returns undefined . 当执行该函数时,它将执行该函数中不存在的"this.string" ,并返回undefined It's a scope issue. 这是一个范围问题。

The solution to make it work good, is to pass the reference which is the object bond 使它工作良好的解决方案是通过引用即对象bond

function combine(num,obj){
    return obj.greeting(num);
}

combine("007",bond); // returns "hello there agent 007"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM