Call function in javascript gives error
This question already has an answer here:
apply
requires an array of parameter, you should use it as
saymyName.apply(o, ['hello there']);
or else you could use call
saymyName.call(o, 'hello there');
Use call instead of apply, as apply needs the second parameter to be an Array–like object.
Use apply when an array of values needs to be sent as parameters to the called function (eg to pass all the arguments of the currently executing function using the arguments object).
Demo
var o = {
name: 'i am object',
age: 22
};
function saymyName(arguentToFunc) {
console.log('my name is ' + this.name + 'and thw argument passed is ' + arguentToFunc);
}
saymyName.call(o, 'hello there');
链接地址: http://www.djcxy.com/p/96986.html
上一篇: 绑定和调用有什么区别?