Javascript:什么是多级原型层次结构,为什么我们应该避免它

Javascript:什么是多级原型层次结构,为什么我们应该避免它,javascript,coding-style,prototype-programming,Javascript,Coding Style,Prototype Programming,在文章中,它说我们不应该使用多级原型层次结构,因为“这些层次结构比它们第一次出现时更难得到正确的结果!”。实际上我不明白它的意思。我在哪里可以找到一个好的例子来解释它的用法和说明它的不良影响?这是因为原型链解析问题。当您有类似于foo.bar的内容时,并不意味着bar属性直接属于foo对象,因此它开始在foo的原型链中搜索bar。如果链很长,则属性解析可能是相对较长的操作。这是因为原型链解析问题。当您有类似于foo.bar的内容时,并不意味着bar属性直接属于foo对象,因此它开始在foo的原型

在文章中,它说我们不应该使用多级原型层次结构,因为“这些层次结构比它们第一次出现时更难得到正确的结果!”。实际上我不明白它的意思。我在哪里可以找到一个好的例子来解释它的用法和说明它的不良影响?

这是因为原型链解析问题。当您有类似于
foo.bar
的内容时,并不意味着
bar
属性直接属于
foo
对象,因此它开始在
foo
的原型链中搜索
bar
。如果链很长,则属性解析可能是相对较长的操作。

这是因为原型链解析问题。当您有类似于
foo.bar
的内容时,并不意味着
bar
属性直接属于
foo
对象,因此它开始在
foo
的原型链中搜索
bar
。如果链很长,则属性解析可能是相对较长的操作。

这是两级继承的一个示例:

// 1. level constructor
var Dog = function ( name ) {
    this.name = name;
};

Dog.prototype.bark = function () { /* ... */ };

// 2. level constructor
var TrainedDog = function ( name, level ) {
    Dog.apply( this, arguments ); // calling the super constructor
    this.level = level;
};

// set up two-level inheritance
TrainedDog.prototype = Object.create( Dog.prototype );
TrainedDog.prototype.constructor = TrainedDog;

TrainedDog.prototype.rollOver = function () { /* ... */ };

// instances
var dog1 = new Dog( 'Rex' );
var dog2 = new TrainedDog( 'Rock', 3 );

这里,
dog1
Dog
原型继承
bark
方法,
dog2
Dog
原型继承该方法,
rollOver
方法从
TrainedDog
原型继承。这是一个两级继承的示例:

// 1. level constructor
var Dog = function ( name ) {
    this.name = name;
};

Dog.prototype.bark = function () { /* ... */ };

// 2. level constructor
var TrainedDog = function ( name, level ) {
    Dog.apply( this, arguments ); // calling the super constructor
    this.level = level;
};

// set up two-level inheritance
TrainedDog.prototype = Object.create( Dog.prototype );
TrainedDog.prototype.constructor = TrainedDog;

TrainedDog.prototype.rollOver = function () { /* ... */ };

// instances
var dog1 = new Dog( 'Rex' );
var dog2 = new TrainedDog( 'Rock', 3 );

这里,
dog1
Dog
原型继承
bark
方法,而
dog2
Dog
原型继承这两种方法以及
TrainedDog
prototype中的
rollOver
方法。

我相信本文指的不是手动设置原型链,而是使用库,如
goog.inherits
util.inherits

你必须手动操作

var Child = function Child() { ... };

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
// for some common value of extend
extend(Child.prototype, {
  ...
});
这可以简化为

var Child = function Child() { ... };

goog.inherits(Child, Parent);
extend(Child.prototype, {
  ...
});

注意这里的
goog.inherits
也处理
对象。在传统浏览器中创建
仿真。

我相信这篇文章指的不是手动设置原型链,而是使用库,如
goog.inherits
util.inherits

你必须手动操作

var Child = function Child() { ... };

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
// for some common value of extend
extend(Child.prototype, {
  ...
});
这可以简化为

var Child = function Child() { ... };

goog.inherits(Child, Parent);
extend(Child.prototype, {
  ...
});

注意这里的
goog.inherits
也处理
对象。在传统浏览器中创建
仿真。

您熟悉原型继承的基础知识吗?这是什么原因?一旦你知道如何正确使用它们,你就没事了。这并不是说你不应该使用它们。它只是说你应该使用图书馆。简短的总结“notpreferred”有误导性,可能是指手工操作。你熟悉原型继承的基础知识吗?这是什么原因?一旦你知道如何正确使用它们,你就没事了。这并不是说你不应该使用它们。它只是说你应该使用图书馆。简短的总结“notpreferred”有误导性,可能指的是手工操作。但这与其说是“正确操作”的问题,不如说是潜在的性能问题。你能再多谈一点吗?在任何明智的浏览器中,这都不是性能问题,原型链是高度优化的。但这与其说是“正确使用”的问题,不如说是潜在的性能问题。你能再多谈一点吗?在任何明智的浏览器中,这都不是性能问题,原型链是高度优化的。