那里发生了什么与原型有关? [英] What is happening there related with prototypes?

查看:82
本文介绍了那里发生了什么与原型有关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我无法理解字符串Cat.prototype = new Animal();做。什么构造函数返回?原型?真的)?

谢谢

<前lang =xml>函数Animal(){
this.eat = function(){
console.log(& amp;#39; eat called& amp;#39;);
};
}

函数Cat(名称){
this.name = name;
};

Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;

var cat = new Cat(& amp;#39; No name cat& amp;#39;);
cat.eat(); //吃掉叫
console.log(cat.name); //没有名字cat
console.log(cat.constructor); // Cat

解决方案

首先,行 Cat.prototype.constructor = Cat; 是多余的,因为它已经 Cat

为了给你一些想法,让我们继续原型链:

  function  Animal(){
this .kingdom = Animalia
}

function CatGenus(){
this .genus = Felis
}
// 让我们跳过家庭,订单和更高的分类:
CatGenus.prototype = new Animal()

function Cat(aName){
this 。 name = aName
this .species = Felis silvestris catus
}
Cat.prototype = new CatGenus()

var cat = new Cat( Pooh



如果您转储对象 cat ,您将看到它具有以下属性:

 name: 维尼 
种类: Felis silvestris catus
genus: Felis
kingdom: Animalia



现在,更糟糕的是你可以把它想象成对象或层次结构。在所有情况下,我建议阅读这篇文章: http://davidwalsh.name/javascript-objects-distractions [ ^ ]。



JavaScript类型与OOP类型有很大不同: https://developer.mozilla。 org / zh-CN / docs / Web / JavaScript / Data_structures [ ^ ]。



JavaScript与此几乎没有共同点;一切都简单得多,但可能更复杂,取决于你来自哪里。你应该考虑对象。对象只是一个关联数组,其属性由键值对表示( https://en.wikipedia .org / wiki / Associative_array [ ^ ],见还 https://en.wikipedia.org/wiki/JavaScript#Dynamic [ ^ ])。所有构造函数都只是函数对象,可能会也可能不会用作构造函数。当您使用函数作为构造函数时,使用 new ,函数将其用作对象的引用。



考虑这个更简单的代码示例:

  var  someObject = {a: 1 ,b: 2 } 
function someFunction(){
return 3 ;
}
someFunction.prototype = someObject
var someDerivedObject = new someFunction();
// 与someObject相同的属性
// 但someObject.constructor未定义

var functionResult = someFunction(); // 只是一个数字



使用this, someFunction 只返回一些结果。但是,没有什么可以阻止你更改原型(在赋值之前 someFunction.prototype = someObject 它已经是一些空但不是未定义的对象)。当通过 new 调用某个函数时,它只是将原型对象作为this,修改它,如果构造函数体对this进行了适当的操作(通常添加一些属性)和返回;返回值被忽略。



了解原型对象被克隆非常重要,而不仅仅是用作参考。要查看它,将属性添加到 someDerivedObject - 它将不会添加到 someObject ,因此可以使用它作为其他对象的原型对象,无论你使用其他派生对象做什么。



参见:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain [ ^ ],
https://developer.mozilla.org/en -US / docs / Web / JavaScript / Reference / Global_Objects / Object / prototype [ ^ ],

https://developer.mozilla.org/en-US/docs/Web/JavaScript / Reference / Global_Objects / Object / isPrototypeOf [ ^ ]。



我建议尝试这些关注第一原则的东西:对象和属性。而不是捕捉想法,想象没有任何预定义的想法是有用的;只有第一原则,它们的使用应该由你自己生成。



-SA


Hello to everyone!
I cannot catch the idea of what does the string Cat.prototype = new Animal(); do. and what does any constructor return? Prototype?really)?
Thank you

function Animal(){
  this.eat = function(){
    console.log(&amp;#39;eat called&amp;#39;);
  };
}

function Cat(name){
  this.name = name;
};

Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;

var cat = new Cat(&amp;#39;No name cat&amp;#39;);
cat.eat(); // eat called
console.log(cat.name); // No name cat
console.log(cat.constructor); // Cat

解决方案

First of all, the line Cat.prototype.constructor = Cat; is redundant, because it's already Cat.
To give you some idea, let's continue prototype chain:

function Animal() {
    this.kingdom = "Animalia"
}

function CatGenus() {
    this.genus = "Felis"
}
// let's skip family, order, and higher taxons:
CatGenus.prototype = new Animal() 

function Cat(aName) {
   this.name = aName
   this.species = "Felis silvestris catus"
}
Cat.prototype = new CatGenus() 

var cat = new Cat("Pooh")


If you dump the object cat, you will see that it has the following properties:

name: "Pooh"
species: "Felis silvestris catus"
genus: "Felis"
kingdom: "Animalia"


Now, the worse thing you could do it to think of it as of object-oriented hierarchy. In all cases, I would recommend to read this article: http://davidwalsh.name/javascript-objects-distractions[^].

The JavaScript types are very different from OOP types: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures[^].

JavaScript has little in common with that; everything is much simpler but maybe more complicated, depending on where you come from. You should think in terms of objects. An object is just an associative array with properties represented by key-value pairs (https://en.wikipedia.org/wiki/Associative_array[^], see also https://en.wikipedia.org/wiki/JavaScript#Dynamic[^]). All the constructors are just function objects which may or may not be used as constructor. When you use a function as a constructor, "new" is used, and function's this is used as the reference to the object.

Consider this simpler code sample:

var someObject = {a:1, b:2}
function someFunction() {
    return 3;
}
someFunction.prototype = someObject
var someDerivedObject = new someFunction();
// same properties as in someObject
// but someObject.constructor is undefined

var functionResult = someFunction(); // just a number 3


Without using "this", someFunction just returns some result. However, nothing prevents you from changing a prototype (before the assignment someFunction.prototype = someObject it already was some "empty" but not undefined object). When some function is called through "new", it simply takes the prototype object as "this", modifies it, if the constructor body has appropriate operations on "this" (usually adds some properties) and returns; the return value is ignored.

It's important to understand that the prototype object is cloned, not just used as a reference. To see it, add a property to someDerivedObject — it won't be added to someObject, so it can be used as a prototype object for other objects, no matter what you do with other "derived" objects.

See also:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf[^].

I would recommend to experiment with this stuff focusing on first principles: objects and properties. Instead of "catching the idea", it would be useful to imagine that there is no any predefined ideas; there are just first principles, and the usage of them should be generated from you, first hand.

—SA


这篇关于那里发生了什么与原型有关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆