Ojbect 和Function 与普通函数和实例对象

1.实例对象的proto 指向构造函数的原型对象

2.实例对象的proto 指向Ojbect的原型

3.所有函数的proto 都指向Function的原型

function定义的对象有一个prototype属性,使用new生成的对象就没有这个prototype属性

prototype属性又指向了一个prototype对象,注意prototype属性prototype对象是两个不同的东西,要注意区别。在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身。

有点头晕,看下图吧:

我们接着看代码:


代码

function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert('I come from prototype.');   
}   
  
var one=new Person('js');   
  
one.showMe();//js,这个结果没有什么好奇怪的   
one.from();//I come from prototype.,这个结果有一点奇怪吧  

要解释这个结果就要仔细研究一下new这个操作符了.var one=new Person('js');这个语句执行的过程可以分成下面的语句:

var one={};   
Person.call(one,'js');  

按照《悟透javascript》书中说的,new形式创建对象的过程实际上可以分为三步:

第一步是建立一个新对象(叫A吧);

第二步将该对象(A)内置的原型对象设置为构造函数(就是Person)prototype 属性引用的那个原型对象;

第三步就是将该对象(A)作为this 参数调用构造函数(就是Person),完成成员设置等初始化工作。

其中第二步中出现了一个新名词就是内置的原型对象,注意这个新名词跟prototype对象不是一回事,为了区别我叫它inobj,inobj就指向了函数Person的prototype对象。在person的prototype对象中出现的任何属性或者函数都可以在one对象中直接使用,这个就是javascript中的原型继承了

又头晕了,上图吧!

这样one对象通过内置的原型对象inobj就可以直接访问Person的prototype对象中的任何属性与方法了。这也就解释了上面的代码中为什么one可以访问form函数了。因为prototype对象中有一个constructor属性,那么one也可以直接访问constructor属性。

__proto__  is inobj????

继承是如何实现的。


代码

function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert('I come from prototype.');   
}   
  
function SubPerson()   
{   
}   
SubPerson.prototype=new Person();   
  
var subOne=new SubPerson();   
subOne.from();//I come from prototype.   
alert(subOne.constructor);//function Person(name) {...};   
alert(SubPerson.prototype.constructor);//function Person(name) {...};  

继承的实现很简单,只需要把子类的prototype设置为父类的一个(实例化)对象即可。注意这里说的可是对象哦!

那么通过prototype属性实现继承的原理是什么呢?还是先看图形说明,然后编写代码进行验证。

注意:红色的方框就是把子类与父类链接起来的地方。这个就应该是传说中的prototype链了吧。下面有代码进行验证。


代码

function Person(name)   
{   
   this.name=name;   
   this.showMe=function()   
        {   
           alert(this.name);   
        }   
};   
  
Person.prototype.from=function()   
{   
  alert('I come from prototype.');   
}   
var father=new Person('js');//为了下面演示使用showMe方法,采用了js参数,实际多采用无参数   
alert(father.constructor);//查看构造函数,结果是:function Person(name) {...};   
function SubPer()   
{   
}   
SubPer.prototype=father;//注意这里   ////在这里SubPer.prototype就指向了father对应的对象
SubPer.prototype.constructor=SubPer;  ////
//这行修改了SubPer.prototype.constructor,其实也给father对象添加了一个constructor属性,此时father.prototype.constructor还是Person,所以father.constructor访问的时候先是找到了自己constructor的属性,就不再去原型里面找
    
var son=new SubPer();   
son.showMe();//js   
son.from();//I come from prototype.   
alert(father.constructor);//function SubPer(){...}   
alert(son.constructor);//function SubPer(){...}   
alert(SubPer.prototype.constructor);//function SubPer(){...}  
 

根据上图的prototype链,还有代码的结果,我想应该明白为什么使用prototype能够实现

JS中的继承了吧。

js function,prototype,sub.的更多相关文章

  1. Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...

  2. prototype.js中Function.prototype.bind方法浅解

    prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...

  3. 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象.         prototype,每一个 ...

  4. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

  5. js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit

    js in depth: Object & Function & prototype & proto & constructor & classes inher ...

  6. JS魔法堂:再次认识Function.prototype.call

    一.前言                                大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello w ...

  7. js中Object.__proto__===Function.prototype

    参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...

  8. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  9. 关于JS的prototype

    在接触JS的过程中,随着理解的深入会逐渐的理解一些比较深奥的理论或者知识,那么今天我们来介绍一下比较难理解的prototype和constructor. 初步理解: 在说prototype和const ...

随机推荐

  1. erlang supervisor simple_one_for_one实例

    simple_one_for_one vs one_for_one: 相同点: 这种Restart Strategy和one_for_one基本相同(即当一个child process挂掉后,仅仅重启 ...

  2. SQLi-Labs学习笔记

    结构化查询语言,也叫做SQL,从根本上说是一种处理数据库的编程语言.对于初学者,数据库仅仅是在客户端和服务端进行数据存储.SQL通过结构化查询,关系,面向对象编程等等来管理数据库.编程极客们总是搞出许 ...

  3. Unknown module(s) in QT: xlsx解决方法

    解决方法在此: https://github.com/dbzhang800/QtXlsxWriter Documentation: http://qtxlsx.debao.me QtXlsx is a ...

  4. FAT,FAT32,NTFS单目录文件数量限制

    http://hi.baidu.com/huaxinchang/item/5ba53ba9b29631756dd4551b —————————————————————————————————————— ...

  5. mui时间选择器选择今天以后的时间

    <script type="text/javascript"> (function($) { $.init(); // var result = $('#result' ...

  6. python import错误 SyntaxError: invalid syntax

    导入一个叫service-listener.py的文件是一直遇到错误: SyntaxError: invalid syntax 单独运行service-listener这个文件时,没有问题,只是不能被 ...

  7. cobbler setting dnsmasq

    Currently cobbler can help generate a ISC DHCP configuration (package name: dhcpd) it can also alter ...

  8. win7 激活相关

    命令 slui 1 slui 2 slui 3 slui 4 slmgr.vbs 需打开的服务 需要开启software protection和 SPP Notification service这两个 ...

  9. 【python】pyqt练习

    import sys from PyQt4.QtCore import * from PyQt4.QtGui import * import ui_price class PriceDlg(QDial ...

  10. MFC中CString.Format的用法

    http://www.cnblogs.com/kongtiao/archive/2012/06/13/2548033.html 在MFC程序中,使用CString来处理字符串是一个很不错的选择.CSt ...