说起这三个属性,肯定有一些同学和我一样,初学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. ubuntu1404_server搭建lamp

    ubuntu server版可直接一键安装lamp环境 apt-get install lamp-server^ 根据提示输入所需设置密码即可,其配置文件跟编译安装的apached等区别很大 apac ...

  2. 系统使用 aspose.cell , 使得ashx第一次访问会变很慢

      网站放在IIS后, 在网站第一次访问后.  回收应用程序池 第一次访问aspx页面还是比较快.   但第一次访问ashx会很慢.   后发现原因: aspose.cell的5.3...版本. 的原 ...

  3. sql server 批量删除数据表

    SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Auth ...

  4. MYSQL注入天书之宽字节注入

    Background-7 宽字节注入 Less-32,33,34,35,36,37六关全部是针对'和\的过滤,所以我们放在一起来进行讨论. 对宽字节注入的同学应该对这几关的bypass方式应该比较了解 ...

  5. Sqli-labs less 41

    Less-41 此处与less-39是一致的,区别在于41错误不回显.所以我们称之为盲注. Payload: http://192.168.11.189/sqli-labs/Less-41/index ...

  6. python模拟shell

    import fileinput import readline raw_input(xxx) exec filepinput.input

  7. pyhton Chapter3 读文件

    使用内置函数open()打开文件,data=open("1.txt").利用data.close()关闭文件.利用data.readline()读取文件中的一行数据,然后指示读取文 ...

  8. CentOS 6下安装nginx

    原文:http://yubosun.akhtm.com/tech/centos-nginx.htm 1 在nginx官方网站下载一个rpm包,下载地址是:http://nginx.org/en/dow ...

  9. POJ 2591 1338 2545 2247(数列递归衍生问题,思路挺妙)

    四道题的难度: 2591<1338<2545<2247 POJ 2591 Set Definition: 这是从discuss里看来的,写的挺好,直接copy,根据我的代码稍有改动( ...

  10. Visual Studio 2015支持为Linux构建应用

    点这里 微软著名的集成开发环境有可能是首次在其产品页提及了竞争对手Linux.Visual Studio 2015的页面声称,“Build for iOS, Android, Windows devi ...