什么都不想说,一段代码两张图,解释一切。注:在此之前请阅读前面的系列博文
对象模型

图片来自于:http://www.cnblogs.com/riccc
红色虚线表示隐式Prototype链。
这张对象模型图中包含了太多东西,不少地方需要仔细体会,可以写些测试代码进行验证。彻底理解了这张图,对JavaScript语言的了解也就差不多了。下面是一些补充说明:
1. 图中有好几个地方提到build-in Function constructor,这是同一个对象,可以测试验证:
|
1
2
3
4
5
6
7
|
//Passed in FF2.0, IE7, Opera9.25, Safari3.0.4Function==Function.constructor //result: trueFunction==Function.prototype.constructor //result: trueFunction==Object.constructor //result: true//Function also equals to Number.constructor, String.constructor, Array.constructor, RegExp.constructor, etc.function fn(){}Function==fn.constructor //result: true |
这说明了几个问题: Function指向系统内置的函数构造器(build-in Function constructor);Function具有自举性;系统中所有函数都是由Function构造。
2. 左下角的obj1, obj2...objn范指用类似这样的代码创建的对象: function fn1(){}; var obj1=new fn1();这些对象没有本地constructor方法,但它们将从Prototype链上得到一个继承的constructor方法,即fn.prototype.constructor,从函数对象的构造过程可以知道,它就是fn本身了。
示例代码
|
1
2
3
4
5
6
7
8
|
//自定义对象代表,对应Javascript Object Model中的use defined functionsfunction Foo(){}//自定义对象创建的对象实例的代表,对应Javascript Object Model中的objects that created by user defined functionsvar foo = new Foo();//String内置函数代表//str为内置函数创建的对象实例的代表,对应Javascript Object Model中的objects that created by build-in constructorsvar str = new String("string"); |
内存展现

你会发现,它和《理解Javascript_09_Function与Object》中的内存分析图是一样的,为什么呢?在《数据模型》中提到过,内置对象都可以看作是函数的派生类型,例如Number instanceof Function为true,Number instanceof Object为true。在这个意义上,可以将它们跟用户定义的函数等同看待。所以内置对象和自定义对象的创建流程是一样的。
在篇博文是在理解了《Function与Object》的基础上写的,因此要理解本文必须理解Function与Object的关系!
原文:http://www.cnblogs.com/villent/p/6369252.html