Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我能';不要更改构造函数中的属性_Javascript_Constructor_Properties_Prototype - Fatal编程技术网

Javascript 我能';不要更改构造函数中的属性

Javascript 我能';不要更改构造函数中的属性,javascript,constructor,properties,prototype,Javascript,Constructor,Properties,Prototype,我是JavaScript的新手,我面临着构造函数的问题,我的问题是我不能用新函数覆盖旧函数的属性 下面是我的代码: 函数myFun(){ this.anotherFun=函数(){ 返回true; } } var myVar=new myFun(); console.log(myVar.anotherFun());//按预期返回“true”; myFun.prototype.anotherFun=函数(){ 返回false; } console.log(myVar.anotherFun());

我是JavaScript的新手,我面临着构造函数的问题,我的问题是我不能用新函数覆盖旧函数的属性

下面是我的代码:

函数myFun(){
this.anotherFun=函数(){
返回true;
}
}
var myVar=new myFun();
console.log(myVar.anotherFun());//按预期返回“true”;
myFun.prototype.anotherFun=函数(){
返回false;
}

console.log(myVar.anotherFun());//is返回'true'为什么不返回'false'?
因为当同一属性在原型链中多次出现时,使用最接近的属性是有意义的

您的实例有自己的属性,因此您无法通过添加继承的属性来覆盖它

您可能不想将
myFun
添加为自己的属性

函数myFun(){}
myFun.prototype.anotherFun=函数(){return true};
var myVar=new myFun();
console.log(myVar.anotherFun());//真的
myFun.prototype.anotherFun=函数(){return false};

console.log(myVar.anotherFun());//false
因为当同一属性在原型链中多次出现时,使用最接近的属性是有意义的

您的实例有自己的属性,因此您无法通过添加继承的属性来覆盖它

您可能不想将
myFun
添加为自己的属性

函数myFun(){}
myFun.prototype.anotherFun=函数(){return true};
var myVar=new myFun();
console.log(myVar.anotherFun());//真的
myFun.prototype.anotherFun=函数(){return false};

console.log(myVar.anotherFun());//false
您尝试执行的操作将无法与您的代码一起使用,因为您自己的属性总是在原型属性之前查找,因此您对原型属性所做的更改将不会产生明显的效果。为了使其工作,您可以将代码更改为始终使用原型属性:

函数myFun(){}
myFun.prototype.anotherFun=函数(){return true;}
var myVar=new myFun();
console.log(myVar.anotherFun());//按预期返回“true”;
myFun.prototype.anotherFun=函数(){return false;}

console.log(myVar.anotherFun());//现在,按预期返回“false”
您尝试执行的操作将无法处理您的代码,因为在原型属性之前始终会查找自己的属性,因此您对原型属性所做的更改将不会产生明显的效果。为了使其工作,您可以将代码更改为始终使用原型属性:

函数myFun(){}
myFun.prototype.anotherFun=函数(){return true;}
var myVar=new myFun();
console.log(myVar.anotherFun());//按预期返回“true”;
myFun.prototype.anotherFun=函数(){return false;}

console.log(myVar.anotherFun());//现在按预期返回'false'。
myFun.prototype.anotherFun=
myFun.anotherFun=您有两个函数,一个自己的属性和一个prototype属性。首先查找dandavis指出的自有属性。
myFun.prototype.anotherFun=
myFun.anotherFun=您有两个函数,一个自有属性和一个prototype属性。正如dandavis指出的,自己的财产首先被查找。谢谢,这对我来说很有用!,但是我们为什么不编写函数myFun(){this.prototype.anotherFun=function(){return true};}而不是函数myFun(){}myFun.prototype.anotherFun=function(){return true}@M.Taki_Eddine,因为没有理由在每次创建实例时运行该代码。所以它应该在构造器之外。谢谢,这对我很有效!,但是我们为什么不编写函数myFun(){this.prototype.anotherFun=function(){return true};}而不是函数myFun(){}myFun.prototype.anotherFun=function(){return true}@M.Taki_Eddine,因为没有理由在每次创建实例时运行该代码。所以它应该在构造器之外。谢谢,这对我很有效!,但是我们为什么不编写函数myFun(){this.prototype.anotherFun=function(){return true};}而不是函数myFun(){}myFun.prototype.anotherFun=function(){return true};–谢谢,这对我有用!,但是我们为什么不编写函数myFun(){this.prototype.anotherFun=function(){return true};}而不是函数myFun(){}myFun.prototype.anotherFun=function(){return true};–