js中函数的js prototype 函数.constructor是指向函数本身,它有什么用

您所在的位置: &
涉及prototype,constructor和instanceof前端js面试难题
涉及prototype,constructor和instanceof前端js面试难题
再来解释instanceof,具体可以参考ECMAScript官方文档和IBM 开发者社区的解释,简而言之,instanceof运算符返回 A 的 prototype 对象是否存在 a 的原型链中。
提问者:自己遇到的前端面试问题,请高手来帮忙解决。其他的感兴趣的同学也可以来看看,自己能不能答对。
var&A&=&function()&{};&A.prototype&=&{};&&var&B&=&{};&console.log(A.constructor);&console.log(B.constructor);&&&&var&a&=&new&A();&A.prototype&=&{};&&var&b&=&new&A();&b.constructor&=&A.&console.log(a.constructor&==&A);&console.log(a.constructor&==&b.constructor);&console.log(a&instanceof&A);&console.log(b&instanceof&A);&
小弟对最后两个console.log的结果不明白,我觉得应该是
console.log(a&instanceof&A);&console.log(b&instanceof&A);&
但是在浏览器中试过了,确实是上面的答案,求解,提前谢谢各位。
首先说明 instanceof 和 constructor 没有半毛钱关系,所以题主的问题有效代码如下:
var&A&=&function()&{};&A.prototype&=&{};&&var&a&=&new&A();&A.prototype&=&{};&var&b&=&new&A();&&console.log(a&instanceof&A);&console.log(b&instanceof&A);&
特别注意我添加的两个注释,对象1和对象2并非同一个对象!
再来解释instanceof,具体可以参考ECMAScript官方文档和IBM 开发者社区的解释,简而言之,instanceof运算符返回 A 的 prototype 对象是否存在 a 的原型链中。
那么上面代码就可以用下面的图示说明:
可以看到,a 的原型链上已经不存在 A 的 prototype 对象,因此console.log(a instanceof A);//false,而 b 的原型链上存在 A 的 prototype 对象,因此console.log(b instanceof A);//true
1) a instancof A 检查a的原型链中是否存在A.prototype
2)每一个js对象都有一个proto属性(标准表示[[prototype]])
proto是普通对象的隐式属性,在new的时候,会指向prototype所指的对象;new出来的对象是没有prototype属性的
proto实际上是某个对象的属性,而prototype则是属于构造函数的属性,prototype指向的是一个实体对象,也就是其有proto属性;
通过proto属性的串联构建了一个对象的原型访问链,起点为一个具体的对象,终点在Object.prototype,其proto( [[ prototype ]]) )为null
3)constrcutor 为对象的构造函数对象,存在于prototype对象(原型对象)中,只要不对prototype对象重新复制,constructor都指向构造函数自身
默认的构造函数为function object()
那么我们来分析下题目
&var&A&=&function()&{};&A.prototype&=&{};&&&&A.prototype&=&{first:'first'};&&var&B&=&{};&console.log(A.constructor);&console.log(B.constructor);&&var&a&=&new&A();&&console.log(a.constructor);&console.log(a.constructor==A);&console.log(a&instanceof&A);&&A.prototype&=&{};&A.prototype&=&{second:&second&};&&&&&console.log(a.constructor==A);&&console.log(a&instanceof&A);&console.log(&a.__proto__&);&&&console.log(a.constructor);&console.log(A.constructor);&&var&b&=&new&A();&console.log(&b.__proto__&);&&b.constructor&=&A.&&&console.log(a.constructor&==&A);&console.log(a.constructor&==&b.constructor);&&&&console.log(a&instanceof&A);&console.log(b&instanceof&A);&
.首先,instanceof 到底比较的什么?
instanceof 比较的是否能在实例的原型对象链中找到 与构造函数(第二个参数)的prototype属性所指向的原型对象,能找到就返回true,反之false;
2. 方法A的原型被篡改为 Object (A.prototype = {})经过这一步之后,
实例 a.[[proto]] = function A(){}.prototype !!//注意,此时这个加粗的prototype已经变成了{}了!
而constructor是原型对象的属性,所以 a.constructor == function Object(){} !!// {}根据自身的原型链找到
A.[[proto]] = function Function(){}.prototype //
A.constructor = function Function(){} //
显然 A.constructor != a.
下一个比较就同理了。
constructor是挂在prototype下的,当A.prototype={}的时候,constructor被删除了。所以a是false,而b又从新设置了constructor指向A,这时候b是true
【责任编辑: TEL:(010)】
关于&&&&的更多文章
如何看将流行的JavaScript MVC框架Ember.js? 这款JavaScript框架
既然强大的Android Studio来了,有什么理由不去用呢?
讲师: 3人学习过讲师: 0人学习过讲师: 0人学习过
苹果在2014WWDC上发布的新编程语言Swift相信牵动着大
在Android应用程序中,可以配置Activity以四种方式来
Web Components是W3C定义的新标准,目前还处于草案阶
本书是一本真正意义上的网络系统设计图书,从网络系统设计角度全面介绍了整个网络系统设计的思路和方法,而不是像传统网络集成类
Windows Phone专家
Android开发专家
51CTO旗下网站网页设计教程与开发
提供各种常见网页效果
提供各种各样的设计教程
装扮QQ,让QQ变得更酷
设计参考,提高自升水平
学习服务器和操作系统
提供各种素材和工具
收藏学习资料
您现在的位置:&&>>&&>>&&>>&&>>&正文
js老生常谈之this,constructor ,prototype全面解析
javascript中的this,constructor ,prototype,都是老生常谈的问题,深入理解他们的含义至关重要。在这里,我们再来复习一下吧,温故而知新!
this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window; 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用。 我们还可以使用apply和call两个全局方法来改变函数中this的具体指向。
先看一个在全局作用范围内使用this的例子:
console.log(this === window); // true
console.log(window.alert === this.alert); // true
console.log(this.parseInt("021", 10)); // 10
函数中的this是在运行时决定的,而不是函数定义时,如下:
// 定义一个全局函数
function foo() {
console.log(this.fruit);
// 定义一个全局变量,等价于window.fruit = "apple";
var fruit = "apple";
// 此时函数foo中this指向window对象
// 这种调用方式和window.foo();是完全等价的
foo(); // "apple"
// 自定义一个对象,并将此对象的属性foo指向全局函数foo
var pack = {
fruit: "orange",
// 此时函数foo中this指向window.pack对象
pack.foo(); // "orange"
全局函数apply和call可以用来改变函数中this的指向,如下:
// 定义一个全局函数
function foo() {
console.log(this.fruit);
// 定义一个全局变量
var fruit = "apple";
// 自定义一个对象
var pack = {
fruit: "orange"
// 等价于window.foo();
foo.apply(window); // "apple"
// 此时foo中的this === pack
foo.apply(pack);
// "orange"
注:apply和call两个函数的作用相同,唯一的区别是两个函数的参数定义不同。
因为在JavaScript中函数也是对象,所以我们可以看到如下有趣的例子:
// 定义一个全局函数
function foo() {
if (this === window) {
console.log("this is window.");
// 函数foo也是对象,所以可以定义foo的属性boo为一个函数
foo.boo = function() {
if (this === foo) {
console.log("this is foo.");
} else if (this === window) {
console.log("this is window.");
// 等价于window.foo();
foo(); // this is window.
// 可以看到函数中this的指向调用函数的对象
foo.boo(); // this is foo.
// 使用apply改变函数中this的指向
foo.boo.apply(window); // this is window.
prototype本质上还是一个JavaScript对象。
并且每个函数都有一个默认的prototype属性。 如果这个函数被用在创建自定义对象的场景中,我们称这个函数为构造函数。 比如下面一个简单的场景:
// 构造函数
function Person(name) {
this.name =
// 定义Person的原型,原型中的属性可以被自定义对象引用
Person.prototype = {
getName: function() {
return this.
var hao= new Person("haorooms");
console.log(hao.getName());
// "haorooms"
作为类比,我们考虑下JavaScript中的数据类型 - 字符串(String)、数字(Number)、数组(Array)、对象(Object)、日期(Date)等。
我们有理由相信,在JavaScript内部这些类型都是作为构造函数来实现的,比如:
// 定义数组的构造函数,作为JavaScript的一种预定义类型
function Array() {
// 初始化数组的实例
var arr1 = new Array(1, 56, 34, 12);
// 但是,我们更倾向于如下的语法定义:
var arr2 = [1, 56, 34, 12];
同时对数组操作的很多方法(比如concat、join、push)应该也是在prototype属性中定义的。 实际上,JavaScript所有的固有数据类型都具有只读的prototype属性 (这是可以理解的:因为如果修改了这些类型的prototype属性,则哪些预定义的方法就消失了), 但是我们可以向其中添加自己的扩展方法。
// 向JavaScript固有类型Array扩展一个获取最小值的方法
Array.prototype.min = function() {
var min = this[0];
for (var i = 1; i & this. i++) {
if (this[i] & min) {
min = this[i];
// 在任意Array的实例上调用min方法
console.log([1, 56, 34, 12].min()); // 1
注意:这里有一个陷阱,向Array的原型中添加扩展方法后,当使用for-in循环数组时,这个扩展方法也会被循环出来。 下面的代码说明这一点(假设已经向Array的原型中扩展了min方法):
var arr = [1, 56, 34, 12];
var total = 0;
for (var i in arr) {
total += parseInt(arr[i], 10);
console.log(total);
解决方法也很简单:
var arr = [1, 56, 34, 12];
var total = 0;
for (var i in arr) {
if (arr.hasOwnProperty(i)) {
total += parseInt(arr[i], 10);
console.log(total);
constructor
constructor始终指向创建当前对象的构造函数。比如下面例子:
// 等价于 var foo = new Array(1, 56, 34, 12);
var arr = [1, 56, 34, 12];
console.log(arr.constructor === Array); // true
// 等价于 var foo = new Function();
var Foo = function() { };
console.log(Foo.constructor === Function); // true
// 由构造函数实例化一个obj对象
var obj = new Foo();
console.log(obj.constructor === Foo); // true
// 将上面两段代码合起来,就得到下面的结论
console.log(obj.constructor.constructor === Function); // true
但是当constructor遇到prototype时,有趣的事情就发生了。 我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。如下例所示:
function Person(name) {
this.name =
Person.prototype.getName = function() {
return this.
var p = new Person("haorooms");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
console.log(p.constructor.prototype.constructor === Person); // true
当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖), constructor的行为就有点奇怪了,如下示例:
function Person(name) {
this.name =
Person.prototype = {
getName: function() {
return this.
var p = new Person("haorooms");
console.log(p.constructor === Person); // false
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false
为什么呢? 原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
Person.prototype = new Object({
getName: function() {
return this.
而constructor始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:
function Person(name) {
this.name =
Person.prototype = {
getName: function() {
return this.
var p = new Person("haorooms");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
function Person(name) {
this.name =
Person.prototype = {
getName: function() {
return this.
Person.prototype.constructor = P
var p = new Person("haorooms");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
也可以这么写:
function Person(name) {
this.name =
Person.prototype = {
constructor:Person,//指定constructor
getName: function() {
return this.
以上这篇js老生常谈之this,constructor ,prototype全面解析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
转载请注明:破洛洛(谢谢合作)
上一篇文章: 下一篇文章:
网友评论:
[][][][][][][][][][]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&JS里Object constructor和Prototype,麻烦解释下_百度知道
JS里Object constructor和Prototype,麻烦解释下
能否形象点解释下,它默认返回 Object 对象的一个实例:对该对象的对象原型的引用。对于所有的对象。对于 Object 对象,该指针指向原始的 Object() 函数Prototype:对创建对象的函数的引用(指针)constructor,复制粘贴的别来了
提问者采纳
如果你学过面向对象的语言或者你知道面向对象.prototype:P/ &#47,&#47,opera等浏览器中可以使用 &#39,那么一个对象可以创建一个实例;/&#47: 对象和实例都可以访问到原型 对象,可以访问到原型(prototype).__proto__ == Person,这应该没有问题; true 对象的原型和实例获取的原型是同一个对象;得到对象的原型 var obj = pro.constructor ,这是前提.&#47.__proto__ ;var pro = P来访问)例如;var proro = p2.__proto__; 可以得到对象原型(prototype)的引用 ;alert(bool),现在开始说重点.__proto__ ; 创建一个对象实例 ;实例。首先js中对象(函数(function)也是对象),chrome.constructor ,然后你通过对象得到的原型(prototype)中也会有一个给对象的指针(即constructor)例如 ;__proto__&#39:function Person(){}。对象实例中也会隐含的包含一个指向prototype的指针(在safrai 。对象创建对象的实例就不多说了 var p = new Person(). //则实例也可以访问到对象p; &#47,总结这要从对象,var bool = p2.prototype,实例和原型三者关系说起:p; 这样可以得到Person对象:var p2 = new Person()./原型可以访问到对象Person
其他类似问题
为您推荐:
constructor的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 构造函数的prototype 的文章

 

随机推荐