js中一切皆为对象 JavaScript中__proto__与prototype的关系
来源:互联网 发布:知乎 原型 axture 编辑:程序博客网 时间:2024/05/29 06:29
Javascript中对象的prototype属性的解释是:返回对象类型原型的引用。
A.prototype = new B();
理解prototype不应把它和继承混淆。A的prototype为B的一个实例,可以理解A将B中的方法和属性全部克隆了一遍。A能使用B的方法和属性。这里强调的是克隆而不是继承。可以出现这种情况:A的prototype是B的实例,同时B的prototype也是A的实例。
一、所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function)
Number.__proto__ === Function.prototype
// true
Boolean.__proto__ === Function.prototype
// true
String.__proto__ === Function.prototype
// true
Object.__proto__ === Function.prototype
// true
Function.__proto__ === Function.prototype
// true
Array.__proto__ === Function.prototype
// true
RegExp.__proto__ === Function.prototype
// true
Error.__proto__ === Function.prototype
// true
Date.__proto__ === Function.prototype
// true
JavaScript中有内置(build-in)构造器/对象共计12个(ES5中新加了JSON),这里列举了可访问的8个构造器。剩下如Global不能直接访问,Arguments仅在函数调用时由JS引擎创建,Math,JSON是以对象形式存在的,无需new。它们的__proto__是Object.prototype。如下
Math.__proto__ === Object.prototype
// true
JSON.__proto__ === Object.prototype
// true
上面说的“所有构造器/函数”当然包括自定义的。如下
// 函数声明
function
Person() {}
// 函数表达式
var
Man =
function
() {}
console.log(Person.__proto__ === Function.prototype)
// true
console.log(Man.__proto__ === Function.prototype)
// true
这说明什么呢?
所有的构造器都来自于Function.prototype,甚至包括根构造器Object及Function自身。所有构造器都继承了Function.prototype的属性及方法。如length、call、apply、bind(ES5)。
Function.prototype也是唯一一个typeof XXX.prototype为 “function”的prototype。其它的构造器的prototype都是一个对象。如下
console.log(
typeof
Function.prototype)
// function
console.log(
typeof
Object.prototype)
// object
console.log(
typeof
Number.prototype)
// object
console.log(
typeof
Boolean.prototype)
// object
console.log(
typeof
String.prototype)
// object
console.log(
typeof
Array.prototype)
// object
console.log(
typeof
RegExp.prototype)
// object
console.log(
typeof
Error.prototype)
// object
console.log(
typeof
Date.prototype)
// object
console.log(
typeof
Object.prototype)
// object
噢,上面还提到它是一个空的函数,alert(Function.prototype) 下看看。
知道了所有构造器(含内置及自定义)的__proto__都是Function.prototype,那Function.prototype的__proto__是谁呢?
相信都听说过JavaScript中函数也是一等公民,那从哪能体现呢?如下
console.log(Function.prototype.__proto__ === Object.prototype)
// true
这说明所有的构造器也都是一个普通JS对象,可以给构造器添加/删除属性等。同时它也继承了Object.prototype上的所有方法:toString、valueOf、hasOwnProperty等。
最后Object.prototype的__proto__是谁?
Object.prototype.__proto__ ===
null
// true
已经到顶了,为null。
二、所有对象的__proto__都指向其构造器的prototype
上面测试了所有内置构造器及自定义构造器的__proto__,下面再看看所有这些构造器的实例对象的__proto__指向谁?
先看看JavaScript引擎内置构造器
var
obj = {name:
'jack'
}
var
arr = [1,2,3]
var
reg = /hello/g
var
date =
new
Date
var
err =
new
Error(
'exception'
)
console.log(obj.__proto__ === Object.prototype)
// true
console.log(arr.__proto__ === Array.prototype)
// true
console.log(reg.__proto__ === RegExp.prototype)
// true
console.log(date.__proto__ === Date.prototype)
// true
console.log(err.__proto__ === Error.prototype)
// true
再看看自定义的构造器,这里定义了一个Person
function
Person(name) {
this
.name = name
}
var
p =
new
Person(
'jack'
)
console.log(p.__proto__ === Person.prototype)
// true
p是Person的实例对象,p的内部原型总是指向其构造器Person的prototype。
每个对象都有一个constructor属性,可以获取它的构造器,因此以下打印结果也是恒等的
function
Person(name) {
this
.name = name
}
var
p =
new
Person(
'jack'
)
console.log(p.__proto__ === p.constructor.prototype)
// true
上面的Person没有给其原型添加属性或方法,这里给其原型添加一个getName方法
function
Person(name) {
this
.name = name
}
// 修改原型
Person.prototype.getName =
function
() {}
var
p =
new
Person(
'jack'
)
console.log(p.__proto__ === Person.prototype)
// true
console.log(p.__proto__ === p.constructor.prototype)
// true
可以看到p.__proto__与Person.prototype,p.constructor.prototype都是恒等的,即都指向同一个对象。
如果换一种方式设置原型,结果就有些不同了
function
Person(name) {
this
.name = name
}
// 重写原型
Person.prototype = {
getName:
function
() {}
}//Person.prototype = {name:"123"} 相当于Person.prototype=new Object({name:"123"} );
var
p =
new
Person(
'jack'
)
console.log(p.__proto__ === Person.prototype)
// true
console.log(p.__proto__ === p.constructor.prototype)
// false
这里直接重写了Person.prototype(注意:上一个示例是修改原型)。输出结果可以看出p.__proto__仍然指向的是Person.prototype,而不是p.constructor.prototype。
这也很好理解,给Person.prototype赋值的是一个对象直接量{getName: function(){}},使用对象直接量方式定义的对象其构造器(constructor)指向的是根构造器Object,Object.prototype是一个空对象{},{}自然与{getName: function(){}}不等。如下
var
p = {}
console.log(Object.prototype)
// 为一个空的对象{}
console.log(p.constructor === Object)
// 对象直接量方式定义的对象其constructor为Object
console.log(p.constructor.prototype === Object.prototype)
// 为true,不解释
上面代码中用到的__proto__目前在IE6/7/8/9中都不支持。IE9中可以使用Object.getPrototypeOf(ES5)获取对象的内部原型。
var
p = {}
var
__proto__ = Object.getPrototypeOf(p)
console.log(__proto__ === Object.prototype)
// true
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
小结:
一、所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function)
Number.__proto__ === Function.prototype
// true
Boolean.__proto__ === Function.prototype
// true
String.__proto__ === Function.prototype
// true
Object.__proto__ === Function.prototype
// true
Function.__proto__ === Function.prototype
// true
Array.__proto__ ===
Function.prototype
// true
RegExp.__proto__ === Function.prototype
// true
Error.__proto__ ===
Function.prototype
// true
Date.__proto__ ===
Function.prototype
// true
说明了Number等都是构造器,这些构造器其实是Function的一个对象。 也就是说相当于 var Number = new Function();
JavaScript中有内置(build-in)构造器/对象共计12个(ES5中新加了JSON),这里列举了可访问的8个构造器。剩下如Global不能直接访问,Arguments仅在函数调用时由JS引擎创建,Math,JSON是以对象形式存在的,无需new。它们的__proto__是Object.prototype。如下
Math.__proto__ === Object.prototype
// true
JSON.__proto__ === Object.prototype
// true
上面说的“所有构造器/函数”当然包括自定义的。如下
// 函数声明
function Person()
{}
// 函数表达式
var Man
=
function()
{}
console.log(Person.__proto__ === Function.prototype)
// true
console.log(Man.__proto__ ===
Function.prototype)
// true
这说明什么呢?
所有的构造器都来自于Function.prototype,甚至包括根构造器Object及Function自身。所有构造器都继承了Function.prototype的属性及方法。如length、call、apply、bind(ES5)。
Function.prototype也是唯一一个typeof XXX.prototype为 “function”的prototype。其它的构造器的prototype都是一个对象。如下
console.log(typeof Function.prototype)
// function
console.log(typeof Object.prototype)
// object
console.log(typeof Number.prototype)
// object
console.log(typeof Boolean.prototype)
// object
console.log(typeof String.prototype)
// object
console.log(typeof Array.prototype)
// object
console.log(typeof RegExp.prototype)
// object
console.log(typeof Error.prototype)
// object
console.log(typeof Date.prototype)
// object
console.log(typeof Object.prototype)
// object
噢,上面还提到它是一个空的函数,alert(Function.prototype) 下看看。
知道了所有构造器(含内置及自定义)的__proto__都是Function.prototype,那Function.prototype的__proto__是谁呢?
相信都听说过JavaScript中函数也是一等公民,那从哪能体现呢?如下
console.log(Function.prototype.__proto__ ===
Object.prototype)
// true
这说明所有的构造器也都是一个普通JS对象,可以给构造器添加/删除属性等。同时它也继承了Object.prototype上的所有方法:toString、valueOf、hasOwnProperty等。
最后Object.prototype的__proto__是谁?
Object.prototype.__proto__ ===
null //
true
已经到顶了,为null。
- js中一切皆为对象 JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系
- JS中__proto__与prototype的关系
- JavaScript中__proto__与prototype的关系深入理解
- JavaScript中__proto__与prototype的关系深入理解
- JavaScript中__proto__与prototype
- javascript中prototype、constructor以及__proto__之间的三角关系
- 详解js中prototype、constructor以及__proto__的关系
- js中__proto__和prototype的区别和关系
- Js中__proto__和prototype的区别和关系
- js中__proto__和prototype的区别和关系?
- Linux 进程间管道pipe通信
- R-pie()
- vc中回车键按下后会跳过scanf的解决方法
- JStorm之Nimbus简介
- 一步一步写算法(之排序二叉树)
- js中一切皆为对象 JavaScript中__proto__与prototype的关系
- 用影子判断方向
- 移动端html5 自动生成下拉刷新控件
- MySQL的binlog日志
- 最简单的视音频播放示例1:总述
- pdf文件怎么转换成word文档
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- 什么是光绘菲林?
- jstl 遍历 List<Hashtable<String,String>>