JavaScript中prototype、__proto__、Function、Object等

来源:互联网 发布:张博士医考网络课登陆 编辑:程序博客网 时间:2024/05/21 21:39

精力的浩瀚、想象的活泼、心灵的勤奋:就是天才。说到prototype,就不得不先说下new的过程。

不是每一个都要站在第一线上的,大家应当做本身份内的工作。我们先看看如许一段代码:

<script type="text/javascript">
var 
Person = function () { };
var p = new Person();
</script>

很简单的一段代码,我们来看看这个new毕竟做了什么?我们可以把new的过程拆分成以下三步:

<1> var p={}; 也就是说,初始化一个对象p。

<2> p.__proto__=Person.prototype;

<3> Person.call(p);也就是说机关p,也可以称之为初始化p。

关键在于第二步,我们来证实一下:

<script type="text/javascript">
var 
Person = function () { };
var p = new Person();
alert(p.__proto__ === Person.prototype);
</script>
?

!注:__proto__这个属性只有在firefox或者chrome浏览器中才是公开容许接见的,是以,其他基于IE内核的浏览器是不会返回true的。


这段代返回true。申明我们步调2的正确。

那么__proto__是什么?我们在这里简单地说下。每个对象都邑在其内部初始化一个属性,就是__proto__,接见一个对象的属性时,若是这个对象内部不存在这个属性,那么他就会去__proto__里找这个属性,这个__proto__又会有本身的__proto__,于是就如许一向找下去,也就是我们日常平凡所说的原型链的概念。

遵守标准,__proto__是不合错误外公开的,也就是说是个私有属性,然则Firefox的引擎将他露出了出来成为了一个共有的属性,我们可以对外接见和设置。

好,概念说清了,让我们看一下下面这些代码:

<script type="text/javascript">
var 
Person = function () { };
Person.prototype.Say = function () {
alert("Person say");
}
var p = new Person();
p.Say();
</script>

这段代码很简单,信赖每小我都如许写过,那就让我们看下为什么p可以接见Person的Say。

起首var p=new Person();可以得出p.__proto__=Person.prototype。那么调用p.Say()时,起首p中没有Say这个属性,于是,他就须要到他的__proto__中去找,也就是Person.prototype,而我们在上方定义了 Person.prototype.Say=function(){}; 于是,就找到了这个办法。

好,接下来,让我们看个更错杂的。

<script type="text/javascript">
var 
Person = function () { };
Person.prototype.Say = function () {
alert("Person say");
}
Person.prototype.Salary = 50000;
var Programmer = function () { };
Programmer.prototype = new Person();
Programmer.prototype.WriteCode = function () {
alert("programmer writes code");
};
Programmer.prototype.Salary = 500;
var p = new Programmer();
p.Say();
p.WriteCode();
alert(p.Salary);
</script>

我们来做如许的推导:

var p=new Programmer()可以得出p.__proto__=Programmer.prototype;

而在上方我们指定了Programmer.prototype=new Person();我们来如许拆分,var p1=new Person();Programmer.prototype=p1;那么:

p1.__proto__=Person.prototype;

Programmer.prototype.__proto__=Person.prototype;

由按照上方获得p.__proto__=Programmer.prototype。可以获得p.__proto__.__proto__=Person.prototype。

好,算清楚了之后我们来看上方的成果,p.Say()。因为p没有Say这个属性,于是去p.__proto__,也就是 Programmer.prototype,也就是p1中去找,因为p1中也没有Say,那就去p.__proto__.__proto__,也就是 Person.prototype中去找,于是就找到了alert(“Person say”)的办法。

其余的也都是同样的事理。

这也就是原型链的实现道理。

最后,其实prototype只是一个假象,他在实现原型链中只是起到了一个帮助感化,换句话说,他只是在new的时辰有着必然的价值,而原型链的本质,其实在于__proto__!

——————————————————————————————————————————————————————

以上涉及到一个概念—原型链,那么什么是原型链呢?

看下面代码及其注释便可懂得。


起首声明:

Function和Object是js中的两个内置对象。js中包含Function在内的所有都是一个Object

Function 本身也是一个“类”,然而,所有“类”都是Funciton的实例,于是 Function instanceof Function; 为true。同时,所有对象都是 Object 类的实例,Object 本身也是一个对象,所有又有 Object instanceof Object 也为 true。别的,还可以认为 Funciton 类型是 Object?? 类型的一个“派生类”,class Function 持续了class Object ,是 class Object 的一个“子类”。看代码:

<script>
var Person = function () { };

?var p = new Person();

alert(p instanceof Object); //true
alert(Person instanceof Function); //true

</script>

?】

?

window.onload = function(){    /*       每个对象实例都有个属性成员用于指向到它的instanceof 对象(暂称为父对象)的原型(prototype)         我们把这种层层指向父原型的关系称为[原型链 prototype chian]         原型也具有父原型,因为它往往也是一个对象实例,除非我们工钱地去改变它                     在JavaScript中,"一切都是对象,函数是第一型。"         Function和Object都是函数的实例。         Function的父原型指向到Function的原型,Function.prototype的父原型是Object的原型         Object的父原型也指向到Function的原型,Object.prototype是所有父原型的顶层               在spiderMonkey引擎中,父原型可以经由过程 __proto__ 进行接见                     大师在看的时辰最后能反复的读几篇,能加深懂得,尤其是原型,父原型,还有原型链的意思.               *  prototype  接见的是原型      *  __proto__  接见的父原型      *  instanceof 原型链的父类               */    Function.prototype.hi = function(){alert(""hi Function"");}    Object.prototype.hi = function(){alert(""hi Object"");}    var a = function()    {        this.txt = ""a"";    };    a.prototype = {        say: function(){alert(""a"");}    };    alert(a instanceof Function); //a是Function的实例;    alert(a.__proto__ == = Function.prototype);    //a的父原型指向到Function的原型;    //a.__proto__父原型 Function    //Function.prototype 父原型 Function    alert(Function instanceof Object);    //Function是Object的实例;    alert(Function.__proto__ == = Function.prototype);    //Function的父原型指向到Function的原型;    alert(Function.prototype.__proto__ == = Object.prototype);    //Function的原型的父原型指向到Object的原型    alert(Object.__proto__ == = Function.prototype);    //Object的父原型指向到Function的原型;    alert(Object.prototype.__proto__);    //Object的原型是所有父原型的顶端,它不再具有父原型,所以成果为null;    alert(a.prototype instanceof Object);    //a的原型也是一个对象    alert(a.prototype.__proto__ == = Object.prototype);    //a的原型的父原型指向Object的原型};

?

?

以上代码描述的关系如下图所示:

?

定义:
1. constructor: Returns a reference to the Object function that created the instance""s prototype.
2. instanceof: The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.

__proto__和prototype的关系:
1. A function""s .prototype is actually the prototype of things made by it, not its prototype. 
2. __proto__ is the actual prototype, but don""t use it. 

JavaScript中class与instance的关系:

原创粉丝点击