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. Android自定义控件之自定义组合控件(三)

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  2. 8.【nuxt起步】-vue组件之间数据交互

    那么现在问题来了,我现在是在index.vue获取了服务端的数据,怎么传值到maincontent.vue?当然你也可以把获取数据放在maincontent.vue,但假如有些数据同时在header, ...

  3. 细说Redis持久化机制

    概述 Redis不仅能够作为缓存来使用,也能够作为内存数据库. Redis作为内存数据库使用时.必需要解决一个问题:数据的持久性.有些将Redis作为缓存使用的场景也需要将缓存的数据持久化到存储介质上 ...

  4. iOS开发之Xcode8兼容适配iOS 10资料整理笔记

    1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...

  5. react request.js 函数封装

    1.request.js  函数封装 import { Toast } from 'antd-mobile'; import axios from 'axios'; import store from ...

  6. Redux作用

    作用:Redux是为了解决React中组件与组件之间数据传递的问题. React组件之间的传递有三种情况:1.父组件传递数据给子组件:由于redux是一个单向数据流的框架,所以它的数据就只能由父组件传 ...

  7. Python web 框架:web.py

    web.py 是一个Python 的web 框架,它简单而且功能强大.web.py 是公开的,无论用于什么用途都是没有限 制的. web.py 安装: pip install web.py 下面开始我 ...

  8. 斯坦福公开课:Developing IOS 8 App with Swift(1-3)心得体会

    最近开始学习Swift开发移动程序.跟随斯坦福大学的公开课进行自学. 这真是一个美好的时代,虽然不能在斯坦福求学,但是可以观看录制的授课录像.讲义,好似老师在给我们上课一样! 心得: 1.每节课信息量 ...

  9. Kubernetes之网络策略(Network Policy)

    系列目录 概述 Kubernetes要求集群中所有pod,无论是节点内还是跨节点,都可以直接通信,或者说所有pod工作在同一跨节点网络,此网络一般是二层虚拟网络,称为pod网络.在安装引导kubern ...

  10. ipython notebook 如何打开.ipynb文件?

    标签: pythontensorflow 2017-03-29 14:17 235人阅读 评论(0) 收藏 举报  分类: TensorFlow(13)  转自:https://www.zhihu.c ...