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. 在intellij中使用checkStyle进行代码规范

    1 编写代码检测规则可以参考阿里和google的规则和checkstyle的官网文档.checkstyle官网地址http://checkstyle.sourceforge.net/ 假设自己的sty ...

  2. Atitit.eclipse git使用

    Atitit.eclipse git使用 1. Git vs svn1 1.1. 直接记录快照,而非差异比较1 1.2. Git的patch则不依附于branch,commit和commit之间的关系 ...

  3. [k8s]组件日志级别说明

    kubectl 执行命令时候 --v 调试, 也可以用作其他组件的 --v配置 参考: https://kubernetes.io/docs/user-guide/kubectl-cheatsheet ...

  4. python学习之range()和xrange()

    在python2中,xrange()返回一个xrange对象,注意这个对象并不是生成器,也不是迭代器,但是是迭代对象. 而range()则返回列表对象. >>> range(10) ...

  5. shader 笔记(一)

  6. hive2.1.1配置

    hive2.1.1配置 首先在conf下复制hive-site.xml <property> <name>javax.jdo.option.ConnectionURL</ ...

  7. eclipse不自动弹出提示的解决办法(eclipse alt+/快捷键失效)centos 6.7

    1.次方法用于没有一点提示的情况:依次打开eclipse上面的windows ——preferences ——java ——editor —— content assist ,在右上方有一行“sele ...

  8. leetcode || 64、Minimum Path Sum

    problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  9. centos7.4 update git

    1. 查看 yum 源仓库的 Git 信息: yum info git 输入如下内容: Available Packages Name : git Arch : x86_64 Version : 1. ...

  10. Have your GDX app run in the web browser

    https://code.google.com/p/libgdx-users/wiki/Applets—————————————————————————————————————————————— Ha ...