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 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects/function/apply') 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];
     }
-- 
cgit v1.2.3-54-g00ecf