说起这三个属性,肯定有一些同学和我一样,初学js时非常困惑,头大,一脸的迷茫。今天就来给大家彻底解决这些担心受怕的问题。

先看this

this定义:

    this就是函数赖以执行的对象。

分析这句话:

  1. this是对象。
2. this依赖函数执行的上下文环境。
3. this存在函数中。 直接看例子: alert(this); //在全局环境调用this, this指向window, 输出[Object window] function Person(){
alert(this);
} 方式一:
Person(); // 全局环境用Person函数, this指向window, 输出[Object window] 方式二:
var obj = new Person(); //把Person当做构造函数, 实例化一个对象
//此时this指向了obj, 不再指向window, 输出[Object object] function Person(){
alert(this.name); //此时无法判断this的身份
} Person(); //this在全局环境中被调用, this.name == window.name, 输出了窗口的名字
var obj = new Person(); //this在obj环境下被调用, this.name == obj.name, 由于name没被赋值, 所以输出undefined 由此可以看出, 我们在阅读代码或者写代码时,看到某个函数中定义的this时, 还无法去判断那个this身份,必须找到它依赖执行的环境(对象)。
再回头看看this的定义,大家就清楚自然了。

 再看constructor和prototype

constructor和prototype的关系非常密切。

constructor是一个对象的属性,这个属性存在在此对象的prototype中, 指向此对象的构造函数。

分析这句话:
1.constructor是一个对象属性。
2.constructor在prototype中
3.constructor指向构造函数 例子1: function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype.getName = function(){
alert(this.name);
}
Person.prototype.getAge = function(){
alert(this.age);
}
var obj = new Person();
alert(obj.constructor == Person);// true
此种方式定义的prototype, constructor是隐藏的, 默认指向Person 例子2:
function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype = {
getName: function(){
alert(this.name);
},
getAge: function(){
alert(this.age);
}
}
var obj = new Person();
alert(obj.constructor == Person);// false 为什么是false? 这种定义prototype, 是把prototype重写了, 覆盖了默认的constructor。
换句话说, 其实这种方式就是给属性重新赋值了, 所以导致默认的constructor被覆盖。
此时的obj.constructor将指向的是Object。 改写一下上面的:
Person.prototype = {
constructor: Person, //强制指向Person
getName: function(){
alert(this.name);
},
getAge: function(){
alert(this.age);
}
}
此时constructor就指向Person了。 prototype是一个函数属性, 此属性同时也是一个对象, 保存着对象实例所共有的属性和方法。 分析这句话:
1.prototype是函数属性, 只要是函数, 就有prototype属性. 而不管是构造函数还是普通函数.
2.prototype同时也是对象.
2.prototype放的是公共的东西, 包括属性和方法. 例子1.
function Person(name, age){
this.name = name;
this.age = age;
} //是函数就有prototype属性, 这个属性也是一个对象
Person.prototype = {
getName: function(){ //所有对象实例都共享
return this.name;
},
getAge: function(){//所有对象实例都共享
return this.age;
}
} var obj = new Person('tom', 23);
obj.getName(); //'tom'
var obj2 = new Person('jack', 23);
obj2.getName(); //'jack'
obj.getName == obj2.getName; //true, 所有实例共享
Person.prototype.getName(); //当做普通函数属性, 根据this定义, 此时this指向的是Person.prototype, 所以返回undefined 以上就是this, constructor, prototype的定义和他们之间的关系. 可能还有些粗糙, 欢迎大家补充. 综合例子: var Tinker = function(){
this.elements = []; };
Tinker.fn = Tinker.prototype = {
constructor: Tinker,
extend: function(obj){
var p;
for(p in obj){
this.constructor.prototype[p] = obj[p];//此处若看明白了, 那么前面的就理解了
}
} }
Tinker.fn.extend({
get: function(){
var length = arguments.length,
i = 0;
for(; i < length; i++){
this.elements.push(document.getElementById(arguments[i])); //此处若看明白了, 那么前面的就理解了
}
return this;//此处若看明白了, 那么前面的就理解了
},
each: function(fn){
var i = 0,
length = this.elements.length;
for(; i < length; i++){
fn.call(this.elements[i], i, this.elements[i]);
}
return this;//此处若看明白了, 那么前面的就理解了
} }); 这个例子其实很简单, 就是向一个对象原型添加方法.一个方法是get, 用于查找页面id. 一个是each, 用于对找到的id元素执行一个方法
//假设有id = 'data', id = 'message'
var obj = new Tinker();
obj.get('data', 'message').each(function(i, item){
    this.style.cssText = 'height:20px; background:#ff0000';
  })

彻底搞清javascript中this, constructor, prototype的更多相关文章

  1. 深入浅析JavaScript中的constructor

    constructor 属性返回对创建此对象的数组函数的引用.本文给大家介绍JavaScript中的constructor ,需要的朋友参考下吧 定义和用法 constructor 属性返回对创建此对 ...

  2. JavaScript中的Array.prototype.slice.call()方法学习

    JavaScript中的Array.prototype.slice.call(arguments)能将有length属性的对象转换为数组(特别注意: 这个对象一定要有length属性). 但有一个例外 ...

  3. 【转】JavaScript中的constructor与prototype

    最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...

  4. JavaScript中__proto__与prototype的关系

    一.所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function) 1 2 3 4 5 6 7 8 9 Number.__proto__ ...

  5. JavaScript中的constructor和继承

    概述 这是我在看JavaScript面向对象编程指南的时候,对constructor和继承的总结. 关于它们的详细知识,可以上网查到,所以我只写那些网上没有的. 内容 constructor的理解 c ...

  6. 【你不知道的javaScript 上卷 笔记7】javaScript中对象的[[Prototype]]机制

    [[Prototype]]机制 [[Prototype]]是对象内部的隐试属性,指向一个内部的链接,这个链接的作用是:如果在对象上没有找到需要的属性或者方法引用,引擎就 会继续在 [[Prototyp ...

  7. JavaScript中__proto__与prototype的关系(转)

    一.所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function) 1 2 3 4 5 6 7 8 9 Number.__proto__ ...

  8. javascript中原型(prototype)与原型链

    javascript是一门动态语言(动态语言Dynamic Programming Language:动态类型语言,意思就是类型的检查是在运行时做的,也就是常说的“弱类型”语言),没有类的概念,有cl ...

  9. [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)

    假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...

随机推荐

  1. 服务器NPC的ID如何分配的

    服务器ID分配包括NPC,Monster,Pet的ID分配都是调用allocateUID然后自动保存的ID加一,pet说是通过玩家的ID移位获得的,调试一下发现还是调用allocateUID,如果通过 ...

  2. 引擎设计跟踪(九.14.2e) DelayLoaded DLLs (/DELAYLOAD)

    关于DLL的delay load: http://msdn.microsoft.com/en-us/library/151kt790.aspx 最近在做GLES的shader compiler, 把现 ...

  3. javascript设计模式--备忘录模式(Memento)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. self._raiseerror(v) File "D:\GameDevelopment\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror

    D:\BaiDuYun\Plist>python unpack_plist.py lobbyRelieveTraceback (most recent call last): File &quo ...

  5. no module named staticfiles

    原地址:http://blog.sina.com.cn/s/blog_77500e110100umms.html 今天想试一下django的Uploadify,找了个例子,运行时出错:myprojec ...

  6. 【☆】javascript数据类型拾遗

    一.Array对象 1.两个数组能用< > == ===做比较吗? 答:数组可以用> <进行矩阵比较,比如a=[1,2,3] b=[1,2,4],那么a<b,如果a的数值 ...

  7. jquey ajax 无刷新提交form

    http://bbs.csdn.net/topics/380237868 $.ajax({ type: "POST", url:ajaxCallUrl, data:$('#your ...

  8. To Use Ubuntuubunt

    1.rename PC; modify the /etc/hostname. 2.Use root account; 1.set the password for root account: sudo ...

  9. Java获取最后插入MySQL记录的自增ID值方法

    方法一: String sql = "INSERT INTO users (username,password,email) VALUES (?,?,?);"; PreparedS ...

  10. 简明Vim练级攻略(转)

    前言今天看到这篇文章,共鸣点非常多.它把Vim使用分为4个级别,目前我自己是熟练运用前面三级的命令,在培养习惯使用第四级.完全就是我这一年来坚持使用Vim的过程.所以不管怎么我要转载这篇文章.翻译自& ...