1、在一般函数中使用 this 指全局对象 window

function fn(){
this.x = 1
}
fn(); //相当于window.fn()

2、作为对象方法使用 this 指该对象

function fn(){
alert(this.x) //this是调用该函数的obj对象 输出test
}
var obj = {'fn':fn,'x':'test'}
obj.fn();

3、作为构造函数使用 this 指new 函数出的对象

function fn(){
this.x = 123; //this是调用该函数的obj对象
}
var obj = new fn(); //obj = {x:123}

4、apply 调用函数,apply 方法作用是改变函数的调用对象,此方法第一个参数为改变后的调用函数的对象,函数里this指第一个参数

var x = 11;
function fn(){
alert(this.x)
}
var obj = {'fn':fn,'x':22}
var obj2 = {'x':33}
obj.fn.apply(); // 11 ,apply()参数为空时,默认调用的是全局对象,this当前指全局对象
obj.fn.apply(obj); // 22
obj.fn.apply(obj2); // 33

JS中 this 的四种用法的更多相关文章

  1. js中this 的四种用法

    this 在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this  ...

  2. JS中this的四种用法

    1.在一般函数方法中使用 this 指代全局对象 2.作为对象方法调用,this 指代上级对象 3.作为构造函数调用,this 指代new 出的对象 4.apply 调用 ,apply方法作用是改变函 ...

  3. javascript中this的四种用法

    javascript中this的四种用法 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-05-11我要评论 在javascript当中每一个function都是一个对象,所 ...

  4. js中this的四种使用方法

    0x00:js中this的四种调用模式 1,方法调用模式 2,函数调用模式 3,构造器调用模式 4,apply.call.bind调用模式 0x01:第一种:方法调用模式 (也就是用.调用的)this ...

  5. 【温故知新】——原生js中常用的四种循环方式

    一.引言 本文主要是利用一个例子,讲一下原生js中常用的四种循环方式的使用与区别: 实现效果: 在网页中弹出框输入0   网页输出“欢迎下次光临” 在网页中弹出框输入1   网页输出“查询中……” 在 ...

  6. C#中 this关键字 四种用法

    /// <summary> /// 主程序入口 /// </summary> /// <param name="args"></param ...

  7. c++中for的四种用法

    #include <algorithm> #include <vector> #include <iostream> using namespace std; in ...

  8. js中继承的几种用法总结(apply,call,prototype)

    一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 <SPAN style="BACKGROUND-COLOR: #ffffff">& ...

  9. js中this的四种调用模式

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. MySQL创建数据库及用户

    create database ${db_name} default charset utf8 COLLATE utf8_general_ci; grant all on ${db_name}.* t ...

  2. TypeError: cannot perform reduce with flexible type

    想要解决这个错误,最好先明白numpy数据类型的dtype转换 生成一个浮点数组 a=np.random.random(4) 输出 a array([0.0945377,0.52199916,0.62 ...

  3. Java消息中间件--初级篇

    一. 为什么使用消息中间件? 假设用户登录系统   传统方式 用户登录  调用短息服务   积分服务  日志服务等各种服务  如果短息服务出现问题就无法发送短信而且用户登录成功必须所有调用全部完成返回 ...

  4. service-worker实践

    service-worker虽然已列入标准,但是支持的浏览器还是有限制,还有比较多的问题. 1. 生命周期 注册成功-------installing--------------> 安装成功(i ...

  5. 【Best Time to Buy and Sell Stock】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  6. leetcode 【 Two Sum 】python 实现

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  7. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  8. Python基础-week02 Python的常用数据类型

    一.模块初识 import导入Py自带模块例如os,sys等及其自己编写的Py文件,导入到其他文件中,默认查找当前目录.如果不在同一目录,会报错,将该自定义py文件模块放到site-packages目 ...

  9. Selenium自动化测试框架的搭建 (转)

    http://www.51testing.com/html/96/86796-851606.html

  10. 【转】ugui自制摇杆

    http://www.cnblogs.com/duyushuang/p/4457691.html 珍爱生命,远离插件. 以上8个字,好好理解. 反正我是这么觉得. 我说的是unity,不是魔兽世界. ...