(三十七)js改变this指向的方法
1.改变函数内部的this指向的三种方法:call(),apply(),bind()
2. 相同点:都可以改变this指向。
<script>
window.color = 'red';
document.color = 'yellow'; var s1 = {color: 'blue' };
function changeColor(){
console.log(this.color);
} changeColor.call(); //red (默认传递参数)
changeColor.call(window); //red
changeColor.call(document); //yellow
changeColor.call(this); //red
changeColor.call(s1); //blue </script>
var Pet = {
words : '...',
speak : function (say) {
console.log(say + ''+ this.words)
}
}
Pet.speak('Speak'); // 结果:Speak...
var Dog = {
words:'Wang'
}
Pet.speak.call(Dog, 'Speak'); //结果: SpeakWang
<script>
window.number = 'one';
document.number = 'two'; var s1 = {number: 'three' };
function changeColor(){
console.log(this.number);
} changeColor.apply(); //one (默认传参)
changeColor.apply(window); //one
changeColor.apply(document); //two
changeColor.apply(this); //one
changeColor.apply(s1); //three </script>
function Pet(words){
this.words = words;
this.speak = function () {
console.log( this.words)
}
}
function Dog(words){
//Pet.call(this, words); //结果: Wang
Pet.apply(this, arguments); //结果: Wang
}
var dog = new Dog('Wang');
dog.speak();
bind()方法使用示例:
this.name="jack";
var demo={
name:"rose",
getName:function(){return this.name;}
} console.log(demo.getName());//输出rose 这里的this指向demo var another=demo.getName;
console.log(another())//输出jack 这里的this指向全局对象 //运用bind方法更改this指向
var another2=another.bind(demo);
console.log(another2());//输出rose 这里this指向了demo对象了;
3. 不同点:接收参数的方式不同。
function add(c,d){
return this.a + this.b + c + d;
}
var s = {a:1, b:2};
console.log(add.call(s,3,4)); // 1+2+3+4 = 10
console.log(add.apply(s,[5,6])); // 1+2+5+6 = 14
<script>
window.firstName = "Cynthia";
window.lastName = "_xie";
var myObject = {firstName:'my', lastName:'Object'};
function getName(){
console.log(this.firstName + this.lastName);
}
function getMessage(sex,age){
console.log(this.firstName + this.lastName + " 性别: " + sex + " age: " + age );
}
getName.call(window); // Cynthia_xie
getName.call(myObject); // myObject getName.apply(window); // Cynthia_xie
getName.apply(myObject);// myObject getMessage.call(window,"女",21); //Cynthia_xie 性别: 女 age: 21
getMessage.apply(window,["女",21]); // Cynthia_xie 性别: 女 age: 21 getMessage.call(myObject,"未知",22); //myObject 性别: 未知 age: 22
getMessage.apply(myObject,["未知",22]); // myObject 性别: 未知 age: 22 </script>
(三十七)js改变this指向的方法的更多相关文章
- 前端js中this指向及改变this指向的方法
js中this指向是一个难点,花了很长时间来整理和学习相关的知识点. 一. this this是JS中的关键字, 它始终指向了一个对象, this是一个指针; 参考博文: JavaScript函数中的 ...
- Javasript中this指向问题和改变this指向的方法
在学习javascript中我们往往会被this的指向问题弄的头昏转向,今天我们就来学习一下this的指向问题,和改变this指向的方法. 一.this的指向问题 在学习this的指向问题之前我们需要 ...
- 【面试题】JS改变this指向的三种方法
一.this指向 点击打开视频讲解更加详细 this随处可见,一般谁调用,this就指向谁.this在不同环境下,不同作用下,表现的也不同. 以下几种情况,this都是指向window 1.全局作用下 ...
- 可以改变this指向的方法
this一般指向的是当前被调用者,但也可以通过其它方式来改变它的指向,下面将介绍三种方式: 1.call用作继承时: function Parent(age){ this.name=['mike',' ...
- this指向及改变this指向的方法
一.函数的调用方式决定了 this 的指向不同,但总的原则,this指的是调用函数的那个对象: 1.普通函数调用,此时 this 指向 全局对象window function fn() { conso ...
- js 改变this指向的三种方法 bind call apply
先了解下bind call apply 的注意点 bind 需要手动调用 第一个参数 this 要指向的对象,后面是 散列的参数 call 不需要手动调用 第一个参数 this 要指向的对象,后面是 ...
- js改变this指向
js中修改this的指向 方法整理 call,apply,bind 以上的三哥方法都是用来改变js中this的指向 call 使用方法:fun.call(thisArg[,arg1[, arg2[, ...
- 改变this指向的三种方法
call.apply.bind三者为改变this指向的方法. 共同点:第一个参数都为改变this的指针.若第一参数为null/undefined,this默认指向window call(无数个参数) ...
- this指向详解及改变它的指向的方法
一.this指向详解(彻底理解js中this的指向,不必硬背) 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是 ...
随机推荐
- java中文转Unicode
public String cnToUnicode(String cn) { char[] chars = cn.toCharArray(); String returnStr = "&qu ...
- for 循环与嵌套
循环:反复执行某段代码.循环四要素:初始条件,循环条件,循环体,状态改变 for(初始条件;循环条件;状态改变){ 循环体} 给出初始条件,先判断是否满足循环条件,如果不满足条件则跳过for语句,如果 ...
- docker内域名无法解析问题
进入 Docker 容器后,在 hosts 文件中,配置域名解析. # 进入 docker 容器 docker exec -it my_web /bin/bash # 修改 hosts 文件 vi / ...
- Python面试题之集合推导式、字典推导式
集合推导式 集合推导式(set comprehensions)跟列表推导式也是类似的, 唯一的区别在于它们使用大括号{}表示. Code: sets = {x for x in range(10)} ...
- Samba 3.6.9 安装、管理
Samba简介 Samba服务类似于windows上的共享功能,可以实现linux上共享文件,windows上访问,当然在linux上可以访问到.是一种在局域网上共享文件和打印机的一种通信协议,它为局 ...
- 20165101刘天野 2018-2019-2《网络对抗技术》Exp2 后门原理与实践
目录 20165101刘天野 2018-2019-2<网络对抗技术>Exp2 后门原理与实践 1. 实验内容 1.1 使用netcat获取主机操作Shell,cron启动 1.2 使用so ...
- 正式学习React(四) ----Redux源码分析
今天看了下Redux的源码,竟然出奇的简单,好吧.简单翻译做下笔记: 喜欢的同学自己可以去github上看:点这里 createStore.js import isPlainObject from ' ...
- thinkphp Composer安装指南
1.首先我们去composer的官网下载composer,当然也可以用命令行的形势下下载,我是在windows安装的.https://www.phpcomposer.com/ 2.下载以后进行安装,一 ...
- Flume-NG源码阅读之SinkGroups和SinkRunner
在AbstractConfigurationProvider类中loadSinks方法会调用loadSinkGroups方法将所有的sink和sinkgroup放到了Map<String, Si ...
- 一些C语言里面的编程
C语言的知识还是不要忘的好: 1.求最大公约数的函数: #include <stdio.h> #define min(a,b) (a)>(b)?(b):(a) int gcd(int ...