博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Object.prototype.constructor
阅读量:5042 次
发布时间:2019-06-12

本文共 2374 字,大约阅读时间需要 7 分钟。

Returns a reference to the  function that created the instance's prototype.

注意这个属性的值是函数本省的引用,而不是一个包含函数名的字符串。这个值只有对primitive values(例如1,true和"test"等)才是只读的。

Description

All objects inherit a constructor property from their prototype:

var o = {};o.constructor === Object; // truevar a = [];a.constructor === Array; // truevar n = new Number(3);n.constructor === Number; // true

Examples

Displaying the constructor of an object

The following example creates a prototype, Tree, and an object of that type, theTree. The example then displays the constructor property for the object theTree.

function Tree(name) {  this.name = name;}var theTree = new Tree('Redwood');console.log('theTree.constructor is ' + theTree.constructor);

This example displays the following output:

theTree.constructor is function Tree(name) {  this.name = name;}

Changing the constructor of an object

The following example shows how to modify constructor value of generic objects. Only true1and "test" will not be affected as they have read-only native constructors. This example shows that it is not always safe to rely on the constructor property of an object.

function Type () {}var types = [  new Array(),  [],  new Boolean(),  true,             // remains unchanged  new Date(),  new Error(),  new Function(),  function () {},  Math,  new Number(),  1,                // remains unchanged  new Object(),  {},  new RegExp(),  /(?:)/,  new String(),  'test'            // remains unchanged];for (var i = 0; i < types.length; i++) {  types[i].constructor = Type;  types[i] = [types[i].constructor, types[i] instanceof Type, types[i].toString()];}console.log(types.join('\n'));

 

This example displays the following output:

function Type() {},false,function Type() {},false,function Type() {},false,falsefunction Boolean() {    [native code]},false,truefunction Type() {},false,Mon Sep 01 2014 16:03:49 GMT+0600function Type() {},false,Errorfunction Type() {},false,function anonymous() {}function Type() {},false,function () {}function Type() {},false,[object Math]function Type() {},false,0function Number() {    [native code]},false,1function Type() {},false,[object Object]function Type() {},false,[object Object]function Type() {},false,/(?:)/function Type() {},false,/(?:)/function Type() {},false,function String() {    [native code]},false,test

 

转载于:https://www.cnblogs.com/RachelChen/p/5420936.html

你可能感兴趣的文章
杰卡德距离
查看>>
7--OC中NSLog函数输出格式详解
查看>>
带着问题学习openstack
查看>>
jmeter分布式运行
查看>>
re的search与match
查看>>
Spring项目的发展历史和SpringBoot的发展历史
查看>>
MVC View 页面手动传递参数显示数据
查看>>
Linux免密远程登陆
查看>>
安装并使用JUnit
查看>>
如何将mysql的路径加入环境变量
查看>>
<Spark Streaming><Flume><Integration>
查看>>
Jmeter之检查点
查看>>
Xilium.CefGlue怎么使用Js调用C#方法
查看>>
剖析servlet injection及源码分析.
查看>>
SAP Dependency 相关性
查看>>
Linux 内存清理
查看>>
アプリ:old basement -地下倉庫からの脱出-脱出ゲーム 攻略
查看>>
Ubuntu的人性化配置
查看>>
POJ-2947 Widget Factory 高斯消元
查看>>
struts2(一)
查看>>