简体   繁体   English

js中编程调用function

[英]Programming call function in js

To exercise in javascript I have decided to write call function myself(calling).为了在 javascript 中锻炼,我决定自己编写调用 function(调用)。 But I can't understand why the following error takes place: Uncaught TypeError: Cannot set property 'calledfunct' of undefined at Function.calling..... Here is the code:但我不明白为什么会发生以下错误: Uncaught TypeError: Cannot set property ' calledfunct' of undefined at Function.calling .....这里是代码:

var alex={
    name:"alex",
    surname:"surname"
}
let nameyourself = function(){
    console.log(`${this.name} ${this.surname}`);
}


Function.prototype.calling=function(smth){
    smth.prototype.calledfunct=this;
    return smth.prototype.calledfunct();
}
nameyourself.calling(alex);

Inside of Function.prototype.calling , the parameter smth refers to alex , and alex doesn't have an attribute called prototype .Function.prototype.calling内部,参数smth指的是alex ,而alex没有一个名为prototype的属性。 Therefore, you are unable to set the property calledfunc of the undefined attribute prototype .因此,您无法设置未定义属性prototypecalledfunc的属性。

If you define a prototype attribute on alex this error should go away.如果你在alex上定义了原型属性,这个错误应该 go 消失。

var alex={
    name:"alex",
    surname:"surname",
    prototype: {}
}

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

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