From 01b0e12ba27b5069248fd09235e9a7143915ee30 Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:02:49 +0800 Subject: remove `notranslate` class in zh-CN --- .../javascript/reference/operators/this/index.html | 46 +++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/operators/this') diff --git a/files/zh-cn/web/javascript/reference/operators/this/index.html b/files/zh-cn/web/javascript/reference/operators/this/index.html index aea931541f..86616407d0 100644 --- a/files/zh-cn/web/javascript/reference/operators/this/index.html +++ b/files/zh-cn/web/javascript/reference/operators/this/index.html @@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Operators/this

语法

-
this
+
this

@@ -32,7 +32,7 @@ translation_of: Web/JavaScript/Reference/Operators/this

无论是否在严格模式下,在全局执行环境中(在任何函数体外部)this 都指向全局对象。

-
// 在浏览器中, window 对象同时也是全局对象:
+
// 在浏览器中, window 对象同时也是全局对象:
 console.log(this === window); // true
 
 a = 37;
@@ -52,7 +52,7 @@ console.log(b)         // "MDN"

因为下面的代码不在严格模式下,且 this 的值不是由该调用设置的,所以 this 的值默认指向全局对象,浏览器中就是 {{domxref("Window", "window")}}。

-
function f1(){
+
function f1(){
   return this;
 }
 //在浏览器中:
@@ -64,7 +64,7 @@ f1() === globalThis;
 
 

然而,在严格模式下,如果进入执行环境时没有设置 this 的值,this 会保持为 undefined,如下:

-
function f2(){
+
function f2(){
   "use strict"; // 这里是严格模式
   return this;
 }
@@ -82,7 +82,7 @@ f2() === undefined; // true
 
 

在类的构造函数中,this 是一个常规对象。类中所有非静态的方法都会被添加到 this 的原型中:

-
class Example {
+
class Example {
   constructor() {
     const proto = Object.getPrototypeOf(this);
     console.log(Object.getOwnPropertyNames(proto));
@@ -102,7 +102,7 @@ new Example(); // ['constructor', 'first', 'second']

不像基类的构造函数,派生类的构造函数没有初始的 this 绑定。在构造函数中调用 {{jsxref("Operators/super", "super()")}} 会生成一个 this 绑定,并相当于执行如下代码,Base为基类:

-
this = new Base();
+
this = new Base();

警告:在调用 super() 之前引用 this 会抛出错误。

@@ -110,7 +110,7 @@ new Example(); // ['constructor', 'first', 'second']

派生类不能在调用 super() 之前返回,除非其构造函数返回的是一个对象,或者根本没有构造函数。

-
class Base {}
+
class Base {}
 class Good extends Base {}
 class AlsoGood extends Base {
   constructor() {
@@ -147,7 +147,7 @@ whatsThis.apply(obj); // 'Custom' 因为函数中的 this 被设置为obj
 
 

this 和对象转换

-
function add(c, d) {
+
function add(c, d) {
   return this.a + this.b + c + d;
 }
 
@@ -164,7 +164,7 @@ add.apply(o, [10, 20]); // 34
 
 

在非严格模式下使用 call 和 apply 时,如果用作 this 的值不是对象,则会被尝试转换为对象。null 和 undefined 被转换为全局对象。原始值如 7 或 'foo' 会使用相应构造函数转换为对象。因此 7 会被转换为 new Number(7) 生成的对象,字符串 'foo' 会转换为 new String('foo') 生成的对象。

-
function bar() {
+
function bar() {
   console.log(Object.prototype.toString.call(this));
 }
 
@@ -176,7 +176,7 @@ bar.call(undefined); // [object global]

ECMAScript 5 引入了 {{jsxref("Function.prototype.bind()")}}。调用f.bind(someObject)会创建一个与f具有相同函数体和作用域的函数,但是在这个新函数中,this将永久地被绑定到了bind的第一个参数,无论这个函数是如何被调用的。

-
function f(){
+
function f(){
   return this.a;
 }
 
@@ -194,7 +194,7 @@ console.log(o.a, o.f(), o.g(), o.h()); // 37, 37, azerty, azerty
 
 

箭头函数中,this与封闭词法环境的this保持一致。在全局代码中,它将被设置为全局对象:

-
var globalObject = this;
+
var globalObject = this;
 var foo = (() => this);
 console.log(foo() === globalObject); // true
@@ -202,7 +202,7 @@ console.log(foo() === globalObject); // true

注意:如果将this传递给callbind、或者apply来调用箭头函数,它将被忽略。不过你仍然可以为调用添加参数,不过第一个参数(thisArg)应该设置为null

-
// 接着上面的代码
+
// 接着上面的代码
 // 作为对象的一个方法调用
 var obj = {foo: foo};
 console.log(obj.foo() === globalObject); // true
@@ -216,7 +216,7 @@ console.log(foo() === globalObject); // true

无论如何,foo 的 this 被设置为他被创建时的环境(在上面的例子中,就是全局对象)。这同样适用于在其他函数内创建的箭头函数:这些箭头函数的this被设置为封闭的词法环境的。

-
// 创建一个含有bar方法的obj对象,
+
// 创建一个含有bar方法的obj对象,
 // bar返回一个函数,
 // 这个函数返回this,
 // 这个返回的函数是以箭头函数创建的,
@@ -252,7 +252,7 @@ console.log(fn2()() == window); // true

下面的例子中,当 o.f() 被调用时,函数内的 this 将绑定到 o 对象。

-
var o = {
+
var o = {
   prop: 37,
   f: function() {
     return this.prop;
@@ -264,7 +264,7 @@ console.log(o.f()); // 37
 
 

请注意,这样的行为完全不会受函数定义方式或位置的影响。在前面的例子中,我们在定义对象o的同时,将其中的函数定义为成员 f 。但是,我们也可以先定义函数,然后再将其附属到o.f。这样做的结果是一样的:

-
var o = {prop: 37};
+
var o = {prop: 37};
 
 function independent() {
   return this.prop;
@@ -279,14 +279,14 @@ console.log(o.f()); // 37
 
 

同样,this 的绑定只受最接近的成员引用的影响。在下面的这个例子中,我们把一个方法g当作对象o.b的函数调用。在这次执行期间,函数中的this将指向o.b。事实证明,这与他是对象 o 的成员没有多大关系,最近的引用才是最重要的。

-
o.b = {g: independent, prop: 42};
+
o.b = {g: independent, prop: 42};
 console.log(o.b.g()); // 42

原型链中的 this

对于在对象原型链上某处定义的方法,同样的概念也适用。如果该方法存在于一个对象的原型链上,那么 this 指向的是调用这个方法的对象,就像该方法就在这个对象上一样。

-
var o = {
+
var o = {
   f: function() {
     return this.a + this.b;
   }
@@ -304,7 +304,7 @@ console.log(p.f()); // 5
 
 

再次,相同的概念也适用于当函数在一个 getter 或者 setter 中被调用。用作 getter 或 setter 的函数都会把 this 绑定到设置或获取属性的对象。

-
function sum() {
+
function sum() {
   return this.a + this.b + this.c;
 }
 
@@ -331,7 +331,7 @@ console.log(o.average, o.sum); // logs 2, 6
 

虽然构造函数返回的默认值是 this 所指的那个对象,但它仍可以手动返回其他的对象(如果返回值不是一个对象,则返回 this 对象)。

-
/*
+
/*
  * 构造函数这样工作:
  *
  * function MyConstructor(){
@@ -370,7 +370,7 @@ console.log(o.a); // logs 38
 
 

当函数被用作事件处理函数时,它的 this 指向触发事件的元素(一些浏览器在使用非 addEventListener 的函数动态地添加监听函数时不遵守这个约定)。

-
// 被调用时,将关联的元素变成蓝色
+
// 被调用时,将关联的元素变成蓝色
 function bluify(e){
   console.log(this === e.currentTarget); // 总是 true
 
@@ -391,14 +391,14 @@ for(var i=0 ; i<elements.length ; i++){
 
 

当代码被内联 on-event 处理函数 调用时,它的this指向监听器所在的DOM元素:

-
<button onclick="alert(this.tagName.toLowerCase());">
+
<button onclick="alert(this.tagName.toLowerCase());">
   Show this
 </button>
 

上面的 alert 会显示 button。注意只有外层代码中的 this 是这样设置的:

-
<button onclick="alert((function(){return this})());">
+
<button onclick="alert((function(){return this})());">
   Show inner this
 </button>
 
@@ -409,7 +409,7 @@ for(var i=0 ; i<elements.length ; i++){

和其他普通函数一样,方法中的 this 值取决于它们如何被调用。有时,改写这个行为,让类中的 this 值总是指向这个类实例会很有用。为了做到这一点,可在构造函数中绑定类方法:

-
class Car {
+
class Car {
   constructor() {
     // Bind sayBye but not sayHi to show the difference
     this.sayBye = this.sayBye.bind(this);
-- 
cgit v1.2.3-54-g00ecf