Home >Web Front-end >JS Tutorial >A good article about javascript-prototype inheritance_prototype

A good article about javascript-prototype inheritance_prototype

WBOY
WBOYOriginal
2016-05-16 19:09:12959browse

1. The most basic usage: Assign an instance of ClassA to ClassB.
ClassB inherits all the attributes of ClassA.
The code is as follows:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

2. From prototype inheritance theory Thinking about it from the perspective,
JS’s prototypal inheritance refers to the prototype, not copying the prototype.
So, modifying the prototype will cause changes in all instances of B.
The code is as follows:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

3. However, subclass objects The write operation only accesses the members in the subclass object.
They will not affect each other. Therefore,
writing is writing to the subclass and reading is reading the prototype (if there is no such thing in the subclass).
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
4. Each subclass object Both have references to the same prototype,

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]5. When constructing a subclass The prototype's constructor will not be executed


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
6. The next step is fatal , access the prototype member object in the subclass object:
<script> function ClassA() { this.a='a'; } function ClassB() { this.b='b'; } ClassB.prototype=new ClassA(); var objB=new ClassB(); for(var p in objB)document.write(p+" "); </script> <script> function ClassA() { this.a='a'; } function ClassB() { this.b='b'; } ClassB.prototype=new ClassA(); var objB=new ClassB(); alert(objB.a); ClassB.prototype.a='changed!!'; alert(objB.a); </script> <script> function ClassA() { this.a='a'; } function ClassB() { this.b='b'; } ClassB.prototype=new ClassA(); var objB1=new ClassB(); var objB2=new ClassB(); objB1.a='!!!'; alert(objB1.a); alert(objB2.a); </script>[Ctrl A select all Note: <script> function ClassA() { this.a=function(){alert();}; } function ClassB() { this.b=function(){alert();}; } ClassB.prototype=new ClassA(); var objB1=new ClassB(); var objB2=new ClassB(); alert(objB1.a==objB2.a); alert(objB1.b==objB2.b); </script>If you need to introduce external Js, you need to refresh to execute <script> function ClassA() { alert("a"); this.a=function(){alert();}; } function ClassB() { alert("b"); this.b=function(){alert();}; } ClassB.prototype=new ClassA(); var objB1=new ClassB(); var objB2=new ClassB(); </script>]<script> function ClassA() { this.a=[]; } function ClassB() { this.b=function(){alert();}; } ClassB.prototype=new ClassA(); var objB1=new ClassB(); var objB2=new ClassB(); objB1.a.push(1,2,3); alert(objB2.a); //所有b的实例中的a成员全都变了!! </script>
7.所以 在prototype继承中 原型类中不能有成员对象! 所有成员必须是值类型数据(string也可以)
用prototype继承有执行效率高,不会浪费内存,为父类动态添置方法后子类中马上可见等的优点。

8.prototype继承是通过把子类的原型对象(prototype)设置成父类的一个实例来进行继承的。

9.prototype继承也有四个比较明显的缺点:
  缺点一:父类的构造函数不是像JAVA中那样在给子类进行实例化时执行的,而是在设置继承的时候执行的,并且只执行一次。这往往不是我们希望的,特别是父类的构造函数中有一些特殊操作的情况下。
  缺点二:由于父类的构造函数不是在子类进行实例化时执行,在父类的构造函数中设置的成员变量到了子类中就成了所有实例对象公有的公共变量。由于JavaScript中继承只发生在“获取”属性的值时,对于属性的值是String,Number和Boolean这些数据本身不能被修改的类型时没有什么影响。但是Array和Object类型就会有问题。
  缺点三:如果父类的构造函数需要参数,我们就没有办法了。
  缺点四:子类原本的原型对象被替换了,子类本身的constructor属性就没有了。在类的实例取它的constructor属性时,取得的是从父类中继承的constructor属性,从而constructor的值是父类而不是子类。

10.可以针对prototype的缺点进行改造
比如把它写成Function对象的一个方法,这样用的时候方便。

Function.prototype.Extends = function (parentClass)
{
  var Bs = new Function();
  Bs.prototype = parentClass.prototype;
  this.prototype = new Bs();
  this.prototype.Super = parentClass;
  this.prototype.constructor = this;
}

希望各位 js 高手能把更好的方式介绍给大家
针对第3,6个

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

针对继承,
Array.prototype 就不能继承 ClassA,ClassB

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

带参数的继承问题

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn