[this] scope

Global Scope

In a browser, the global scope is the window object.

if in your code you simply have: var x = 9; You’re actually setting the property window.x to 9 (when working in a browser). You could type window.x = 9;

Local Scope

JavaScript scopes at a function level.

1234
function () {	var x = 5;};console.log(x); 

If you declare a variable & forget to use the var keyword, that variable is automatically made global. So this code would work:

1234
function () {	x = 5;});console.log(x); //5

this

this is a variable that is automatically set for you when a function is invoked.

Depending on how a function is invoked, this is set differently:

123
function foo() {	console.log(this); //global object};
1234
myapp = {};myapp.foo = function() {	console.log(this); //points to myapp object}
1234
var link = document.getElementById("myId");link.addEventListener("click", function() {	console.log(this); //points to link}, false);

在函数内部时,this由函数怎么调用来确定。

Simple call

简单调用,即独立函数调用。由于this没有通过call来指定,且this必须指向对象,那么默认就指向全局对象。

12345
function f1(){  return this;}

f1() === window; // global object

在严格模式下,this保持进入execution context时被设置的值。如果没有设置,那么默认是undefined。它可以被设置为任意值(包括null/undefined/1等等基础值,不会被转换成对象)。

123456
function f2(){; // see strict mode  return this;}

f2() === undefined;

大专栏  [this] scope-functions">Arrow functions

在箭头函数中,this由词法/静态作用域设置(set lexically)。它被设置为包含它的execution context的this,并且不再被调用方式影响(call/apply/bind)。

1234567891011121314
var globalObject = this;var foo = (() => this);console.log(foo() === globalObject); // true

// Call as a method of a objectvar obj = {foo: foo};console.log(obj.foo() === globalObject); // true

// Attempt to set this using callconsole.log(foo.call(obj) === globalObject); // true

// Attempt to set this using bindfoo = foo.bind(obj);console.log(foo() === globalObject); // true

As an object method

当函数作为对象方法调用时,this指向该对象。

12345678
var o = {  prop: 37,  f: function() {    return this.prop;  }};

console.log(o.f()); // logs 37

this on the object’s prototype chain

原型链上的方法根对象方法一样,作为对象方法调用时this指向该对象。

构造函数

在构造函数(函数用new调用)中,this指向要被constructed的新对象。

call和apply

Function.prototype上的callapply可以指定函数运行时的this

1234567
function add(c, d){  return this.a + this.b + c + d;}

var o = {a:1, b:3};add.call(o, 5, 7); // 1 + 3 + 5 + 7 = 16add.apply(o, [10, 20]); // 1 + 3 + 10 + 20 = 34

注意,当用callapply而传进去作为this的不是对象时,将会调用内置的ToObject操作转换成对象。所以4将会装换成new Number(4),而null/undefined由于无法转换成对象,全局对象将作为this

bind

ES5引进了Function.prototype.bindf.bind(someObject)会创建新的函数(函数体和作用域与原函数一致),但this被永久绑定到someObject,不论你怎么调用。

As a DOM event handler

this自动设置为触发事件的dom元素。

参考链接

从这两套题,重新认识JS的this、作用域、闭包、对象
深入理解JS中声明提升、作用域(链)和this关键字

原文地址:https://www.cnblogs.com/liuzhongrong/p/12271935.html

时间: 2024-08-29 21:13:44

[this] scope的相关文章

一招制敌 - 玩转 AngularJS 指令的 Scope (作用域),讲得特别好

学习了AngularJS挺长时间,最近再次回首看看指令这部分的时候,觉得比自己刚开始学习的时候理解的更加深入了,尤其是指令的作用域这部分. 步入正题: 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部的Controller提供的作用域或者根作用域($rootScope)),还是创建一个新的自己的作用域,当然AngularJS为我们指令的scope参数提供了三种选择,分别是:false,true,{}:默认情况下是false. scope = false 首先我们来看

Angular this vs $scope

this vs $scope ------------------------------------------------------------------------------ 'this' vs $scope in AngularJS controllers How does this and $scope work in AngularJS controllers? Short answer: this When the controller constructor functio

微信公众平台开发教程(十)Scope参数错误或没有Scope权限解决方法

一 报错信息: 二 出现原因分析: 出现这种错误网上查出现有的原因是: 1. 订阅号没有相关的权限 2. 账号没有认证,没有相关的权限 那么这里遇到问题两种都不是.开发账号是 服务号,而且也是认证号. 三 解决方案: 需要在OAuth2.0网页授权中配置授权回调页面域名. 如: 1.首先检查您的微信号是服务号还是订阅号,如果是订阅号恭喜你中奖了,因为腾讯没开放接口出来. 在开发者中心: 2.确保你已是服务号,且已通过微信认证. 注: 授权回调域名配置规范为全域名并且不带http,比如需要网页授权

modelsim中,错误 Error: already declared in this scope ()

仿真软件modelsim中,错误 Error: already declared in this scope () 在定义这个信号前其它模块接口信号中调用了这个信号,modelsim仿真报错,通过把信号定义挪到调用模块前面问题解决. 可能是modelsim有要求,在块里边出现之前,必须先做声明.modelsim中,错误 Error: already declared in this scope (),布布扣,bubuko.com

聊一下JS中的作用域scope和闭包closure

scope和closure是javascript中两个非常关键的概念,前者JS用多了还比较好理解,closure就不一样了.我就被这个概念困扰了很久,无论看别人如何解释,就是不通.不过理越辩越明,代码写的多了,小程序测试的多了,再回过头看看别人写的帖子,也就渐渐明白了闭包的含义了.咱不是啥大牛,所以不搞的那么专业了,唯一的想法就是试图让你明白什么是作用域,什么是闭包.如果看了这个帖子你还不明白,那么多写个把月代码回过头再看,相信你一定会有收获:如果看这个帖子让你收获到了一些东西,告诉我,还是非常

微信 登录 Scope 参数错误或没有 Scope 权限

//电脑端 扫码授权登录 public static string AuthUrl = "https://open.weixin.qq.com/connect/qrconnect?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_login&state={2}#wechat_redirect"; //移动端 直接授权登录 public static string AuthUrl = &q

ng $scope与$rootScope的关系

$scope与$rootScope的关系:①不同的控制器之间 是无法直接共享数据②$scope是$rootScope的子作用域对象$scope的id是随着控制器的加载顺序依次递增,$rootScope的id是1 ③不同控制器之间如何通信??1.借助于$rootScope2.既然子作用域对象可以调用父作用域对象的值或者方法,就可以通过控制器之间的嵌套来实现通信3.事件父->子$scope.$broadcast('eventName',args) 子->父$scope.$emit('eventNa

Scope in AngularJS

From the RUNBOOM.COM, it said: Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Scope 可应用在视图和控制器上. While, how many scopes can we have? is there any structure in it? I got those question when I come to it. After a tough time r

Scope Directive

---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/scope What are Scopes? Scope is an object that refers to the application model. It is an execution context for expressions. Scopes are arranged in hiera

Spring(二)scope、集合注入、自动装配、生命周期

原文链接:http://www.orlion.ga/189/ 一.scope bean的scope属性中常用的有两种:singleton(单例,默认)和prototype(原型,每次创建新对象) 例:beans.xml <bean id="userService" class="ml.orlion.service.UserService" scope="prototype">     <property name="u