js 函数中的this
function 函数
没有“this”的持久概念, 调用函数时,创建this
function hello(thing) {
console.log(this + " says hello " + thing);
}
person = { name: "Brendan Eich" }
person.hello = hello;
// 在person中调用 hello, this被创建到了person
person.hello("world") // still desugars to person.hello.call(person, "world")
// 在window中调用 this被创建到了 window
hello("world") // "[object DOMWindow]world"
引用具有持久this值的函数
name = "global";
var person = {
name: "Brendan Eich",
hello: function() {
console.log(this.name);
}
};
var boundHello = function(arguments) {
return person.hello.apply(person, arguments);
};
boundHello(); // Brendan Eich
let a = person.hello.bind(person);
a(); // Brendan Eich
let b = person.hello
b() // global
箭头函数
箭头函数捕获this => 创建函数的位置 而不是调用它的位置
箭头函数没有自己的
this, 箭头函数内部的上下文在函数的整个生命周期中保持不变, 并且始终将this绑定到最接近的非箭头父函数中的上下文
let o = {
a: 'ajanuw',
hello: function(){
// 返回箭头函数时,this已经被绑定了
return () => {
console.log(this.a)
}
}
}
let a = o.hello()
a()
js 函数中的this的更多相关文章
- 在JS函数中执行C#中的函数、字段
1.调用字段 cs文件的代码: ; protected void Page_Load(object sender, EventArgs e) { id = ; } js页面的代码: function ...
- js函数中获得当前被点击元素
问题描述:在html页面中点击<a>或者’按钮‘,进入js中的函数,在js函数中获得被点击那个<a>或‘按钮’元素 解决方法:方法一: html中: <a>标签:& ...
- js函数中this的不同含义
1.js函数调用过程中,js线程会进入新的执行环境并创建该环境的变量对象,并添加两个变量:this和arguments,因此可以在函数中使用这两个变量.需要注意的是,this变量不能重新赋值,而arg ...
- js函数中参数的传递
数据类型 在 javascript 中数据类型可以分为两类: 基本类型值 primitive type,比如Undefined,Null,Boolean,Number,String. 引用类型值,也就 ...
- js函数中的BOM和DOM
BOM 浏览器对象模型 screen对象 console.log(screen.width);// 屏幕宽度 console.log(screen.height);// 屏幕高度 console.l ...
- 深入理解JS函数中this指针的指向
函数在执行时,会在函数体内部自动生成一个this指针.谁直接调用产生这个this指针的函数,this就指向谁. 怎么理解指向呢,我认为指向就是等于.例如直接在js中输入下面的等式: console.l ...
- 将从model中获得的数据传到js函数中
刚遇到了一种情况,从controller中获得的model是一个集合,需要将这个集合循环放到标签中,并且需要为这些标签添加点击事件,每个值传入对应的点击事件函数中,由于model中的值是通过${ite ...
- js函数中的this关键字
关于这个this关键字,也是很多项目中常常被用到的,那么,有人也许会问,干嘛要用this呢,在函数被调用时,直接指明是什么对象在调用不就行了?还整那么个模模糊糊的概念出来干嘛?不过嘛,存在即真理,既然 ...
- js函数中写默认值的几种方式(常见的)
<script> <!--第一种写法,我更喜欢第一种写法直观一些--> function Person(name){ this.name = name || '默认名字乔丹'; ...
随机推荐
- gdb调试动态链接so
http://blog.csdn.net/weed_hz/article/details/12710429 gdb) file <你的exe>(gdb) load <你的so> ...
- 控制WinForm中Tab键的跳转
一,需求 在Winform中,默认情况下,按下Tab键,光标会按照我们设定的TabIndex值从小到大进行跳转. 但如果用户要求按下Tab键跳转到特定的控件,这种要求还是很合理的,比如用户只想输入几个 ...
- 透明Panel
unit TransparentPanel; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Varia ...
- [Asp.net core]使用ssh命令发布asp.net core项目
命令 # 移除之前发布的包 rm -rf ./.Publish rm -rf ./Wolfy.Blog.tar.gz # 编译并发布 将发布包打包在.Publish目录下 -o "../.P ...
- 已安装nginx动态添加模块
说明:已经安装好的nginx,需要添加一个未被编译安装的模块,需要怎么弄呢? 具体:这里以安装第三方ngx_http_google_filter_module模块为例nginx的模块是需要重新编译ng ...
- Window下使用Charles对手机的Https请求进行抓包
https://blog.csdn.net/zhaoerduo/article/details/52128607
- 解决Android Studio出现Failed to open zip file. Gradle's dependency cache may be corrupt的问题
问题如下图所示: 解决: 修改 gradle-wrapper.properties里的gradle的版本,与之前没有报错的gradle版本一致.就可以了 比如我报这个错的时候 : distributi ...
- Install Redis 3.2 on Ubuntu
Install Redis 3.2 on Ubuntu It’s very easy to install Redis 3 on Ubuntu 16, just need to add PPA rep ...
- 转 : 深入解析Java锁机制
深入解析Java锁机制 https://mp.weixin.qq.com/s?__biz=MzU0OTE4MzYzMw%3D%3D&mid=2247485524&idx=1&s ...
- Windows系统下安装zabbix客户端
简单介绍如何在windows系统下安装zabbix客户端 1. 首先下载和zabbix服务端大版本相同的windows客户端 例如我服务端安装的是zabbix-3.4.14.tar.gz ...