简体   繁体   English

如何通过构造函数的实例访问构造函数的属性?

[英]How can I access a property of a constructor through an instance of that constructor?

Here the constructor is Animal.这里的构造函数是 Animal。 It has two instances duck and beagle.它有两个实例duck 和beagle。 There is a function named eat() which is actually a prototype of Animal.有一个名为eat() 的function 实际上是Animal 的原型。

function Animal() {
  this.color = "brown";
 }

Animal.prototype = {
  constructor: Animal,
  eat: function() {
    console.log("nom nom nom");
  }
};

let duck = Object.create(Animal.prototype); 
let beagle = Object.create(Animal.prototype); 
duck.eat();
console.log(duck.color);

here这里

duck.eat() 

works, but duck must also inherit the color brown right?有效,但鸭子也必须继承棕色,对吗? Why am I not able to access it using为什么我无法使用它访问它

duck.color ?

There is no color property on Animal.prototype . Animal.prototype上没有color属性。 It is dynamically added to the object when the constructor function is called… but you aren't calling the constructor function at all.当构造函数 function 被调用时,它被动态添加到 object 中……但你根本没有调用构造函数 function。

If you want to create an instance of a class, then call the constructor function.如果要创建 class 的实例,则调用构造函数 function。 Don't use Object.create .不要使用Object.create

 function Animal() { this.color = "brown"; } Animal.prototype = { eat: function() { console.log("nom nom nom"); } }; let duck = new Animal(); let beagle = new Animal(); duck.eat(); console.log(duck.color);

No It wont inherit,不,它不会继承,

Please read the definition of Object.create .请阅读Object.create的定义。

"The Object.create() method creates a new object, using an existing object as the prototype of the newly created object" “Object.create() 方法创建一个新的 object,使用现有的 object 作为新创建对象的原型”

Means, only prototype will be copied from existing object on will be placed in newly create object's prototype意味着,只有prototype will be copied from existing object on will be placed in newly create object's prototype

Here color is instance variable not prototype.这里color是实例变量而不是原型。 Hence wont be available in newly created duck object.因此在新创建的duck object 中将不可用。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

暂无
暂无

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

相关问题 如何访问构造函数中的构造成员? - How can I access a constructor member within a constructor function? 如何使用构造函数的实例变量访问java脚本中构造函数的静态属性? - How to access static property of constructor in java-script using instance variable of constructor? 如何在Ramda中访问构造函数'this'? - How can I access constructor 'this' inside Ramda? 如何在不使用 this 关键字的情况下访问 Javascript 中同一类的方法内的类的构造函数中定义的属性? - How can I access a property defined in a constructor of a class inside a method of the same class in Javascript without using this keyword? 如何在运行时访问Typescript类的公共属性(调试)? 仅构造函数和函数可访问 - how can I access typescript class public property at run time (debugging)? only the constructor and functions are accessible 如何通过构造函数属性而不是new调用函数构造函数 - How to invoke function constructor through constructor property instead of new JS,原型:具有构造函数,如何获取实例? - JS, prototype: Having a constructor, how can I get an instance? 如何在使用构造函数进行原型设计时访问实例? - How to access the instance when prototyping with constructor functions? 如何为传递给构造函数的对象设置默认属性? - How can I set a default property for an object that's passed into a constructor? 如何在javascript的构造函数中覆盖继承对象的属性? - How can I override an inherited object's property in the constructor in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM