在js中this的使用方法非常让人迷惑。有些像Java或者C#中的this,但又不全然一样。依照流行的说法this总是指向调用方法的对象。

1、纯粹函数调用。

 function ListCommon2(x)
{
this.x=x;
alert("this 是 ListCommon2"+(this instanceof ListCommon2));
alert("this.constructor"+this.constructor);
}
function test(){
//測试代码
var t1=ListCommon2("烧水");
var t2=new ListCommon2("烧水2");
}

经过測试发现,

假设不使用new,也就是var t1=ListCommon2("烧水");这样调用,this是全局对象Window对象,

假设使用new,也就是var
t2=new ListCommon2("烧水2");;这样调用,this就变成了new出来的实例对象的应用,也就是 t2,

看来和调用方式也是有关系的。

2、作为方法调用,那么this就是指实例化的对象。

 function ListCommon2(x)
{
this.x=x;
this.Do=function(x)//特权方法 实例方法
{
alert("this 是 ListCommon2"+(this instanceof ListCommon2));
alert("this.constructor"+this.constructor);
}
}
ListCommon2.prototype.Do2=function()//实例方法
{ alert("this 是 ListCommon2"+(this instanceof ListCommon2));
alert("this.constructor"+this.constructor);
} function test(){
//測试代码
var t1=ListCommon2("烧水");
// t1.Do();调用错误
// t1.Do2();调用错误
var t2=new ListCommon2("烧水2");
t2.Do();
t2.Do2();
}

经过測试发现。无论是特权方法类型的实例方法,还是原型类型的实例方法,this都指向了当前新创建的对象。

3 apply,call调用

apply的第一个參数就是this。假设没有传递this就是全局对象。

改变this的方法。通过new能够改变,使用call和apply也能够改变

4 setTimeout中的this

 function ListCommon2(x)
{
this.x=x;
this.Do=function(x)//特权方法 实例方法
{
window.setTimeout(function(){
alert("this 是 ListCommon2"+(this instanceof ListCommon2));
alert("this.constructor"+this.constructor); },100);
}
}
ListCommon2.prototype.Do2=function()//实例方法
{
window.setTimeout(function(){
alert("this 是 ListCommon2"+(this instanceof ListCommon2));
alert("this.constructor"+this.constructor); },100);
} function test(){
//測试代码
var t2=new ListCommon2("烧水2");
t2.Do();
t2.Do2();
}

測试发现setTimeout中的this也是全局对象Window对象。当然这种样例还有非常多,感觉应该是实例化的对象,可实际上却不是。因此须要注意。

參考文章

js中this的使用方法

js中this的总结

js面向对象编程:this究竟代表什么?的更多相关文章

  1. 页面循环绑定(变量污染问题),js面向对象编程(对象属性增删改查),js字符串操作,js数组操作

    页面循环绑定(变量污染问题) var lis = document.querySelectorAll(".ul li") for ( var i = 0 ; i < lis. ...

  2. js面向对象编程 ---- 系列教程

    原 js面向对象编程:数据的缓存 原 js面向对象编程:如何检测对象类型 原 js面向对象编程:if中可以使用那些作为判断条件呢? 原 js面向对象编程:this到底代表什么?第二篇 原 js面向对象 ...

  3. 带你一分钟理解闭包--js面向对象编程

    上一篇<简单粗暴地理解js原型链--js面向对象编程>没想到能攒到这么多赞,实属意外.分享是个好事情,尤其是分享自己的学习感悟.所以网上关于原型链.闭包.作用域等文章多如牛毛,很多文章写得 ...

  4. Js面向对象编程

    Js面向对象编程 1.     什么是面向对象编程? 我也不说不清楚什么是面向对象,反正就那么回事吧. 编程有时候是一件很快乐的事,写一些小游戏,用编程的方式玩游戏等等 2.     Js如何定义一个 ...

  5. js原生设计模式——3简单工厂模式\js面向对象编程实例

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

  6. JS面向对象编程(进阶理解)

    JS 面向对象编程 如何创建JS对象 JSON语法声明对象(直接量声明对象) var obj = {}; 使用 Object 创建对象 var obj = new Object(); JS对象可以后期 ...

  7. 简单粗暴地理解js原型链–js面向对象编程

    简单粗暴地理解js原型链–js面向对象编程 作者:茄果 链接:http://www.cnblogs.com/qieguo/archive/2016/05/03/5451626.html 原型链理解起来 ...

  8. JS面向对象编程,对象,属性,方法。

    document.write('<script type="text/javascript" src="http://api.map.baidu.com/api?v ...

  9. js面向对象编程(第2版)——js继承多种方式

    附带书籍地址: js面向对象编程(第2版)

  10. 原生js面向对象编程-选项卡(自动轮播)

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

随机推荐

  1. navigator.geolocation在IOS10系统中无法定位问题

    在使用地图需要定位的朋友都可能遇到这个问题,参考的文章说的是用百度地图,我用的是腾讯地图,但是其中的原理差不多.所以谢谢这些提供资源的大神们. if (navigator.geolocation) { ...

  2. Back Track 5 之 网络踩点

    DNS记录探测 dnsenum 针对NDS信息收集的工具 格式: ./dnsenum.pl dbsserver (域名) 请原谅我拿freestu.net这个学校团委的域名做的测试,求不黑!! dns ...

  3. SQL-重复记录查询的几种方法

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from ...

  4. PreferenceScreen的应用

    PreferenceScreen preference是偏好.首选的意思,PreferenceScreen个人翻译成 "偏好显示",明确这个意思就好.就是说依据特点灵活的定义显示内 ...

  5. Could not find com.android.tools.build:gradle:3.0.0-alpha1 in circle ci

      Error:(1, 0) The android gradle plugin version 3.0.0-alpha1 is too old, please update to the lates ...

  6. Carrer Day有感

    2013年10月17日 一.永不放弃,痼疾必须自己克服,否则永远有阴影. 二.若长期无法取得进展,应该换一种方式. 大Boss确实有水平,答到点子上且照顾到听众面子,强.很多话掷地有声,发人深省,比如 ...

  7. WPF代码模板-布局部分

    Grid 两行和三列 <Grid ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition ...

  8. MySQL查看表结构三种方法

    1:desc T1 2:EXPLAIN T1 3:SHOW COLUMNS FROM T1

  9. Java 基础【12】 日期类型

    java api中日期类型的继承关系 java.lang.Object --java.util.Date --java.sql.Date --java.sql.Time --java.sql.Time ...

  10. use of _track and track_visibility

    Dosen't work...the followers don't recieve an email when the state is change. Here is the code in th ...