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 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,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的更多相关文章

  1. Object & prototype & __proto__ All In One

    Object & prototype & proto All In One js 原型,原型链,原型对象 const obj ={}; // {} const obj = new Ob ...

  2. JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()

    1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...

  3. 深入理解Javascript中this, prototype, constructor

    在Javascript面向对象编程中经常需要使用到this,prototype和constructor这3个关键字. 1.首先介绍一下this的使用:this表示当前对象;如果在全局中使用this,则 ...

  4. prototype/constructor/__proto__之constructor。

    1.constructor的字面意思就是构造.它是对象的一个属性,它对应的值是该对象的“构造者” //一.构造函数实例化的对象的constructor function Cmf(n,m){ this. ...

  5. Object.prototype.propertyIsEnumerable

    语法: obj.propertyIsEnumerable(prop); 此方法返回一个布尔值,表明指定的属性名是否是当前对象可枚举的自身属性. 1.如果是用户自定义了对象的属性,将返回true,比如 ...

  6. 图形验证码知识点整理 Object.prototype.toString.call()等

    使用typeof bar === "object"检测”bar”是否为对象有什么缺点?如何避免?这是一个十分常见的问题,用 typeof 是否能准确判断一个对象变量,答案是否定的, ...

  7. Object.prototype的成员介绍

    3.Object.prototype的成员介绍        Object.prototype是js中所有的对象的祖宗        Object.prototype中所有的成员都可以被js中所有的对 ...

  8. 关于Object.prototype.toString.call

    slice(8,-1)意思是从第8位开始(包含第8位)到最后一位之前(-1的意思就是最后一位,不包含最后一位): Object.prototype.toString.call(boj)这个是用来判断数 ...

  9. 原型模式Prototype,constructor,__proto__详解

    最近由于在找工作,又拿起<JavaScript高级程序设计>看了起来,从中也发现了自己确实还是有很多地方不懂,刚刚看到原型模式这里,今天终于搞懂了,当然,我也不知道自己的理解是否有错. 1 ...

随机推荐

  1. iOS -- 设置label的自适应

    - (void)AutoLabel { //准备工作 self.font = [UIFont systemFontOfSize:]; self.textColor = [UIColor whiteCo ...

  2. [C++11]_[0基础]_[左值引用声明和右值引用声明]

    场景: 在 remove_reference 结构体中能看到右值引用的身影 &&, 那么这里的右值引用究竟有什么用呢? 常常也发现int& 和int&& 这两种 ...

  3. nexus启动报错-----&gt;错误 1067: 进程意外终止。

    1.今天启动nexus报错: 2.错误信息 错误 1067: 进程意外终止. 3.检查发现我之前把jdk升级了. 然而nexus之前指定的jdk将不再生效. 4.解决的方法 找到nexus安装文件夹 ...

  4. sql的一些知识_where

    简单相同查询,查询age=22的三列信息,并按照降序排列 ORDER BY weight DESC 此外,where还支持一下判断操作符 值得注意的是,如果比较的值为数值,则不需要加引号,如果是字符, ...

  5. gulp在ionic中的使用

    简介 Gulp是一个基于流的自动化构建器. 安装 npm config set registry http://registry.npm.taobao.org ---最好用国内源 npm instal ...

  6. rf-demos (request)

    *** Settings *** Library RequestsLibrary Library Collections Library XML *** Test Cases *** case1 Cr ...

  7. python--文件操作补充

    文件操作补充 f = open('file',encoding='utf-8')其中打开时不写模式默认只读f是文件句柄,文件操作符,不能使数字相当于content = f.read()content是 ...

  8. docker&k8s填坑记

    本篇主要用于记录在实施docker和kubenetes过程中遇到的一个问题和解决办法. 本节部分内容摘自互联网,有些部分为自己在测试环境中遇到到实际问题,后面还会根据实际情况不断分享关于docker/ ...

  9. Selenium学习(二)入门小例子

    1)打开百度页面 2)输入“hello” 3)点击百度一下按钮 from selenium import webdriver url = "http://" + "www ...

  10. 【TensorFlow-windows】(零)TensorFlow的"安装"

    Tensorflow的安装,具体操作就不演示了.具体操作请移步: http://blog.csdn.net/darlingwood2013/article/details/60322258#comme ...