constrctor,prototype,__proto__

constructor

定义和用法

在 JavaScript 中, constructor 属性返回对象的构造函数。 返回值是函数的引用,不是函数名:

true.constructor                //function Boolean() { [native code] }
'1'.constructor                 //function String() { [native code] }
1.constructor                   //VM404:1 Uncaught SyntaxError: Invalid or unexpected token
(1).constructor                 //function Number() { [native code] }
[].constructor                  //function Array() { [native code] }
function a(){} a.constructor    //function Function() { [native code] }

typeof true                     //'boolean'
typeof 1                        //'number'
typeof '1'                      //'string'
function a(){} typeof a         //'function'
typeof []                       //'object'

1 instanceof Number             //false
new Number(1) instanceof Number //true
'1' instanceof String           //false
new String(1) instanceof String //true
[] instanceof Array             //true
new Array(1) instanceof Array   //true

复制代码

注1:除 null,undefined 都有constrctor属性,说明js是基于对象的

注2:typeof操作符返回一个字符串,表示未经计算的操作数的类型。

注2:instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的prototype 属性。[].__proto__.constrctor:Array()

prototype

定义和用法

prototype 属性使您有能力向对象添加属性和方法。 当构建一个属性,所有的实例将被设置属性,它是默认值。 在构建一个方法时,所有的实例都可以使用该方法。

true.prototype                //undefined
'1'.prototype                 //undefined
(1).prototype                 //undefined
[].prototype                  //undefined
function a(){} a.prototype    //{constructor: ƒ}

Boolean.prototype             //Boolean {[[PrimitiveValue]]: false}
String.prototype              //String {length: 0, [[PrimitiveValue]]: ""}
Number.prototype              //Number {[[PrimitiveValue]]: 0}
Array.prototype               //[]
Function.prototype            //function () {}
复制代码

注1:prototype 是定义在构造函数上,为实例添加属性和方法

注2:Boolean,String,Number,Array 都重写了 toString,valueOf 方法

注3:实例对象可以直接访问 prototype中添加的属性和方法

__proto__

定义和用法

原型继承

true.__proto__                 //Boolean {[[PrimitiveValue]]: false}
'1'.__proto__                  //String {length: 0, [[PrimitiveValue]]: ""}
(1).__proto__                  //Number {[[PrimitiveValue]]: 0}
[].__proto__                   //[]
function a(){} a.__proto__     //function () {}

Boolean.__proto__             //function () {}
String.__proto__              //function () {}
Number.__proto__              //function () {}
Array.__proto__               //function () {}
Function.__proto__            //function () {}
Object.__proto__              //function () {}

true.__proto__   === Boolean.prototype  //true
'1'.__proto__    === String.prototype   //true
(1).__proto__    === Number.prototype   //true
[].__proto__     === Array.prototype    //true
function a(){}
a.__proto__      === Function.prototype //true
Object.__proto__ === Function.prototype //true
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值