Object.prototype.constructor
Returns a reference to the Object 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; // true
var a = [];
a.constructor === Array; // true
var 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 true, 1and "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,false
function Boolean() {
[native code]
},false,true
function Type() {},false,Mon Sep 01 2014 16:03:49 GMT+0600
function Type() {},false,Error
function Type() {},false,function anonymous() {
}
function Type() {},false,function () {}
function Type() {},false,[object Math]
function Type() {},false,0
function Number() {
[native code]
},false,1
function Type() {},false,[object Object]
function Type() {},false,[object Object]
function Type() {},false,/(?:)/
function Type() {},false,/(?:)/
function Type() {},false,
function String() {
[native code]
},false,test
Object.prototype.constructor的更多相关文章
- Object & prototype & __proto__ All In One
Object & prototype & proto All In One js 原型,原型链,原型对象 const obj ={}; // {} const obj = new Ob ...
- JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()
1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...
- 深入理解Javascript中this, prototype, constructor
在Javascript面向对象编程中经常需要使用到this,prototype和constructor这3个关键字. 1.首先介绍一下this的使用:this表示当前对象;如果在全局中使用this,则 ...
- prototype/constructor/__proto__之constructor。
1.constructor的字面意思就是构造.它是对象的一个属性,它对应的值是该对象的“构造者” //一.构造函数实例化的对象的constructor function Cmf(n,m){ this. ...
- Object.prototype.propertyIsEnumerable
语法: obj.propertyIsEnumerable(prop); 此方法返回一个布尔值,表明指定的属性名是否是当前对象可枚举的自身属性. 1.如果是用户自定义了对象的属性,将返回true,比如 ...
- 图形验证码知识点整理 Object.prototype.toString.call()等
使用typeof bar === "object"检测”bar”是否为对象有什么缺点?如何避免?这是一个十分常见的问题,用 typeof 是否能准确判断一个对象变量,答案是否定的, ...
- Object.prototype的成员介绍
3.Object.prototype的成员介绍 Object.prototype是js中所有的对象的祖宗 Object.prototype中所有的成员都可以被js中所有的对 ...
- 关于Object.prototype.toString.call
slice(8,-1)意思是从第8位开始(包含第8位)到最后一位之前(-1的意思就是最后一位,不包含最后一位): Object.prototype.toString.call(boj)这个是用来判断数 ...
- 原型模式Prototype,constructor,__proto__详解
最近由于在找工作,又拿起<JavaScript高级程序设计>看了起来,从中也发现了自己确实还是有很多地方不懂,刚刚看到原型模式这里,今天终于搞懂了,当然,我也不知道自己的理解是否有错. 1 ...
随机推荐
- android 获取GPS定位
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- Understand the Business Domain
 Understand the Business Domain Mark Richards EFFECTivE SoFTWARE ARCHiTECTS understand not only tec ...
- windows下配置redis集群,启动节点报错:createing server TCP listening socket *:7000:listen:Unknown error
windows下配置redis集群,启动节点报错:createing server TCP listening socket *:7000:listen:Unknown error 学习了:https ...
- Chrome内核保存为mhtml(单网页)
在地址栏输入:chrome://flags 回车 然后Ctrl+f查找mhtml Tips: 如果网页图片看不太清可以CTRL+鼠标滚轮放大网页 如果系统原因以及其它因素可以下载:QQ浏览器(默认保 ...
- log4j:WARN Please initialize the log4j system properly解决的方法
要解决问题很easy,建立LOG4J 的配置文件就可以. 在src 文件夹下创建配置文件,选 一.择菜单File > New > File,文件名称输入log4j.properties,文 ...
- Android 扫描Scard卡全部的图片
这几天为了扫描Scard卡全部的图片的事非常纠结,我原本以为这是一件非常easy的事.可是我发现我错了.网上也没有完整的代码.仅仅是零零碎碎的能扫描单个文件的代码.在今天代码调试通过之后,我认为我有必 ...
- Asp.Net初学小结 判断数组中是否有重复的数据
Asp.Net初学小结 第一章 1.搭建Asp.net开发环境 1).net FrameWork(VS) 2)IIS(xp:5.1,2003:6.0,vista:70,win7:7.5) ...
- soap webservice
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://cnn237111.blog.51cto.com/2359144/1357029 ...
- 搭建React Native开发环境
搭建React Native开发环境 本文档是Mac下搭建的环境,针对的目标平台不同,以及开发 iOS 和 Android 的不同,环境搭建也有差异. Github地址:https://github. ...
- elk 日志分析系统Logstash+ElasticSearch+Kibana4
elk 日志分析系统 Logstash+ElasticSearch+Kibana4 logstash 管理日志和事件的工具 ElasticSearch 搜索 Kibana4 功能强大的数据显示clie ...