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 --- .../reference/global_objects/function/apply/index.html | 18 +++++++++--------- .../reference/global_objects/function/bind/index.html | 16 ++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects/function') diff --git a/files/zh-cn/web/javascript/reference/global_objects/function/apply/index.html b/files/zh-cn/web/javascript/reference/global_objects/function/apply/index.html index d345215619..944791e29c 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/function/apply/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/function/apply/index.html @@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Function/apply

语法

-
func.apply(thisArg, [argsArray])
+
func.apply(thisArg, [argsArray])

参数

@@ -58,7 +58,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Function/apply

apply正派上用场!

-
var array = ['a', 'b'];
+
var array = ['a', 'b'];
 var elements = [0, 1, 2];
 array.push.apply(array, elements);
 console.info(array); // ["a", "b", 0, 1, 2]
@@ -70,7 +70,7 @@ console.info(array); // ["a", "b", 0, 1, 2]
 
 

下面是示例,我们将用Math.max/Math.min求得数组中的最大/小值。

-
/* 找出数组中最大/小的数字 */
+
/* 找出数组中最大/小的数字 */
 var numbers = [5, 6, 2, 3, 7];
 
 /* 使用Math.min/Math.max以及apply 函数时的代码 */
@@ -91,7 +91,7 @@ for (var i = 0; i < numbers.length; i++) {
 
 

如果你的参数数组可能非常大,那么推荐使用下面这种混合策略:将数组切块后循环传入目标方法:

-
function minOfArray(arr) {
+
function minOfArray(arr) {
   var min = Infinity;
   var QUANTUM = 32768;
 
@@ -110,7 +110,7 @@ var min = minOfArray([5, 6, 2, 3, 7]);
 
 

你可以使用apply来链接一个对象构造器,类似于Java。在接下来的例子中我们会创建一个全局Function 对象的construct方法 ,来使你能够在构造器中使用一个类数组对象而非参数列表。

-
Function.prototype.construct = function (aArgs) {
+
Function.prototype.construct = function (aArgs) {
   var oNew = Object.create(this.prototype);
   this.apply(oNew, aArgs);
   return oNew;
@@ -122,7 +122,7 @@ var min = minOfArray([5, 6, 2, 3, 7]);
 
 

Using {{jsxref("Object/__proto__", "Object.__proto__")}}:

-
Function.prototype.construct = function (aArgs) {
+
Function.prototype.construct = function (aArgs) {
   var oNew = {};
   oNew.__proto__ = this.prototype;
   this.apply(oNew, aArgs);
@@ -131,7 +131,7 @@ var min = minOfArray([5, 6, 2, 3, 7]);
 
 

使用闭包:

-
Function.prototype.construct = function(aArgs) {
+
Function.prototype.construct = function(aArgs) {
   var fConstructor = this, fNewConstr = function() {
     fConstructor.apply(this, aArgs);
   };
@@ -141,7 +141,7 @@ var min = minOfArray([5, 6, 2, 3, 7]);
 
 

使用 Function 构造器:

-
Function.prototype.construct = function (aArgs) {
+
Function.prototype.construct = function (aArgs) {
   var fNewConstr = new Function("");
   fNewConstr.prototype = this.prototype;
   var oNew = new fNewConstr();
@@ -152,7 +152,7 @@ var min = minOfArray([5, 6, 2, 3, 7]);
 
 

使用示例:

-
function MyConstructor (arguments) {
+
function MyConstructor (arguments) {
     for (var nProp = 0; nProp < arguments.length; nProp++) {
         this["property" + nProp] = arguments[nProp];
     }
diff --git a/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html b/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html
index c612abb1df..8940da5ad4 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Function/bind
 
 

语法

-
function.bind(thisArg[, arg1[, arg2[, ...]]])
+
function.bind(thisArg[, arg1[, arg2[, ...]]])

参数

@@ -63,7 +63,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Function/bind

bind() 最简单的用法是创建一个函数,不论怎么调用,这个函数都有同样的 this 值。JavaScript新手经常犯的一个错误是将一个方法从对象中拿出来,然后再调用,期望方法中的 this 是原来的对象(比如在回调中传入这个方法)。如果不做特殊处理的话,一般会丢失原来的对象。基于这个函数,用原始的对象创建一个绑定函数,巧妙地解决了这个问题:

-
this.x = 9;    // 在浏览器中,this 指向全局的 "window" 对象
+
this.x = 9;    // 在浏览器中,this 指向全局的 "window" 对象
 var module = {
   x: 81,
   getX: function() { return this.x; }
@@ -85,7 +85,7 @@ boundGetX(); // 81
 
 

bind() 的另一个最简单的用法是使一个函数拥有预设的初始参数。只要将这些参数(如果有的话)作为 bind() 的参数写在 this 后面。当绑定函数被调用时,这些参数会被插入到目标函数的参数列表的开始位置,传递给绑定函数的参数会跟在它们后面。

-
function list() {
+
function list() {
   return Array.prototype.slice.call(arguments);
 }
 
@@ -120,7 +120,7 @@ var result3 = addThirtySeven(5, 10);
 
 

在默认情况下,使用 {{ domxref("window.setTimeout()") }} 时,this 关键字会指向 {{ domxref("window") }} (或 global)对象。当类的方法中需要 this 指向类的实例时,你可能需要显式地把 this 绑定到回调函数,就不会丢失该实例的引用。

-
function LateBloomer() {
+
function LateBloomer() {
   this.petalCount = Math.ceil(Math.random() * 12) + 1;
 }
 
@@ -145,7 +145,7 @@ flower.bloom();  // 一秒钟后, 调用 'declare' 方法

绑定函数自动适应于使用 {{jsxref("Operators/new", "new")}} 操作符去构造一个由目标函数创建的新实例。当一个绑定函数是用来构建一个值的,原来提供的 this 就会被忽略。不过提供的参数列表仍然会插入到构造函数调用时的参数列表之前。

-
function Point(x, y) {
+
function Point(x, y) {
   this.x = x;
   this.y = y;
 }
@@ -178,7 +178,7 @@ new YAxisPoint(17, 42) instanceof Point; // true

请注意,你不需要做特别的处理就可以创建一个和 {{jsxref("Operators/new", "new")}} 操作符一起使用的绑定函数。也就是说,你不需要做特别处理就可以创建一个可以被直接调用的绑定函数,即使你更希望绑定函数是用 {{jsxref("Operators/new", "new")}} 操作符来调用。

-
// ...接着上面的代码继续的话,
+
// ...接着上面的代码继续的话,
 // 这个例子可以直接在你的 JavaScript 控制台运行
 
 // 仍然能作为一个普通函数来调用
@@ -196,7 +196,7 @@ emptyObj.x + ',' + emptyObj.y;   //  '0,13'
 
 

你可以用 {{jsxref("Array.prototype.slice")}} 来将一个类似于数组的对象(array-like object)转换成一个真正的数组,就拿它来举例子吧。你可以简单地这样写:

-
var slice = Array.prototype.slice;
+
var slice = Array.prototype.slice;
 
 // ...
 
@@ -204,7 +204,7 @@ slice.apply(arguments);

bind()可以使这个过程变得简单。在下面这段代码里面,slice 是 {{jsxref("Function.prototype")}} 的 {{jsxref("Function.prototype.apply()", "apply()")}} 方法的绑定函数,并且将 {{jsxref("Array.prototype")}} 的 {{jsxref("Array.prototype.slice()", "slice()")}} 方法作为 this 的值。这意味着我们压根儿用不着上面那个 apply()调用了。

-
// 与前一段代码的 "slice" 效果相同
+
// 与前一段代码的 "slice" 效果相同
 var unboundSlice = Array.prototype.slice;
 var slice = Function.prototype.apply.bind(unboundSlice);
 
-- 
cgit v1.2.3-54-g00ecf