(三十七)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的最终指向的是 ...
随机推荐
- 笔记-mysql 导出查询结果
语法: The SELECT ... INTO OUTFILE 'file_name' [options] form of SELECT writes the selected rows to a f ...
- hadoop16---反射
框架配置文件中,从字符串获取类和实例,调他的方法. 通过反射的方式可以获取class对象中的属性.方法.构造函数等,一下是实例: package cn.itcast_04_reflect; impor ...
- JavaWeb请求中文乱码
解决中文乱麻问题,页面端发出的数据作两次encodeURI var name="张三"; encodeURI(encodeURI(name)); 后台解码: URLDecoder. ...
- div 文章内容自动分屏显示
<head runat="server"> <title></title> <script language="javascri ...
- InterruptedException异常
本文总结自:https://blog.csdn.net/asdfsadfasdfsa/article/details/78808131 什么样的方法会抛出InterruptedException异常? ...
- Python-GUI编程-PyQt5
Python-GUI编程-PyQt5 1. GUI编程是什么? GUI 全称为: Graphical User Interface;简称GUI翻译为中文为: 图形化用户接口简单理解就是:- 使用Pyt ...
- 华为S5700系列交换机使用高级ACL限制不同网段的用户互访
组网图形 图1 使用高级ACL限制不同网段的用户互访示例 组网需求 如图一所示,某公司通过Switch实现各部门之间的互连.为方便管理网络,管理员为公司的研发部和市场部规划了两个网段的IP地址.同时为 ...
- BZOJ-5424: 烧桥计划(单调队列)
BZOJ-5424: 烧桥计划(单调队列) 题目链接 题解: 先考虑最暴力的\(dp\):设\(f[k][i]\)表示搞掉第\(1\sim i\)段,烧了\(k\)段的最小花费,设\(calc(x,y ...
- Coldfusion8 读取HASH工具
#!/usr/bin/env python #-*- coding:utf- -*- import sys import threading import urllib import httplib ...
- JNI_Z_07_方法的操作(没有String类型的参数)_参数的传递方式
1. 2.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_TjniMethod02.h" JNIEXPORT void ...