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 ...
随机推荐
- sql server 博客
http://blog.csdn.net/tjvictor/article/category/531421/1 http://blog.csdn.net/zhangqidashu/article/de ...
- 产品如何进行大屏数据可视化.md
最近接到一个需求,需要给公司的竞赛平台面对省/校/竞赛进行大屏的可视化话数据展示,闲暇之余对自己最近的工作进行一些总结; 一.数据可视化的定义 数据可视化主要是关于数据_视觉表现形式的科学技术研究 - ...
- springboot配置filter
Filter 过滤器是web开发中很重要的一个组件,下面以一个session登陆的例子介绍下spring boot中如何使用Filter 首先要准备一个实现了Filter的接口的类 SessionFi ...
- remove xcode recent projects from dock menu 移除xcode dock菜单显示的项目列表
Launch Xcode Select File->Open Recent->Clear Menu Right-click the Xcode icon and select Show A ...
- SilverLight-DataBinding:二、Bingding to a Collection Objects(绑定一个集合对象)
ylbtech-SilverLight-DataBinding: Bingding to a Collection Objects(绑定一个集合对象) 1.A, Building a Data Ob ...
- Gvim 和 Opencv编译
好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...
- AngularJS的Foreach循环示例
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularJSForeach.rar 代码: <!DOCTYPE HTML PUBLIC ...
- Solaris Samba服务器与DNS服务
用于文件传输的协议,类似于ftp,ssh,只是它比其他两个好用. Samba协议 NetBIOS :一种编程接口. SMB:server message block .主要作为Microsoft网络通 ...
- 为什么应使用 Node.js
为什么应使用 Node.js JavaScript 高涨的人气带来了很多变化,以至于如今使用其进行网络开发的形式也变得截然不同了.就如同在浏览器中一样,现在我们也可以在服务器上运行 JavaScrip ...
- BUCK电路工作原理
Buck电路,也称呼为DC_DC Buck型降压开关电源电路,这种电路结构实际应用也是很多的,电路拓扑结构看下图: 电路中,Q1是开关管,D1是续流二极管,L1就是问题中提到的这个电感器.C1就是问题 ...