河南截获大量日本草莓种苗

Baseline Widely available
百度 浓度为56微克/立方米,同比下降%;PM10浓度为91微克/立方米,同比下降%。

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

bind() ???? ???? ??? ??? ?????. ???? ? ??? value?? this ???? ????, ???? ???? ???? ??? ??? ?????.

??? ??

const module = {
  x: 42,
  getX: function () {
    return this.x;
  },
};

const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// Expected output: undefined

const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// Expected output: 42

??

js
    func.bind(thisArg[, arg1[, arg2[, ...]]])

????

thisArg

??? ??? ?? ??(target function)? this? ???? ????. ??? ??? new ???? ??? ?? ?????. bind? ???? setTimeout ?? ?? ??? ?? ?, thisArg? ??? ?? ?? ??? ?????. bind? ??(argument)? ???? ??? ?? ??? ?? this? ??? ??? thisArg? ?????.

arg1, arg2, ...

?? ??? ?? ?? ??? ??.

?? ?

??? this ? ? ?? ??? ???? ??? ?? ??? ???.

??

bind() ??? ??? ???? ??? ????. ???? ??? ?? ?? ??? ??? ???, ECMAScript 2015?? ??? ?? ?? ??(exotic function object)???. ???? ??? ???? ????? ??? ??? ?? ???.

???? ??? ??? ?? ?? ??? ??? ????.

  • [[BoundTargetFunction]] - ????? ??(wrapped) ?? ?? ??.
  • [[BoundThis]] - ??? ??? ???? ? ?? ???? ?.
  • [[BoundArguments]] - ??? ??? ??? ? ? ?? ??? ???? ??? ??.
  • [[Call]] - ? ??? ??? ?? ??. ?? ?? ?? ?? ???. ?? ???? ??? this ? ? ?? ??? ??? ???? ??? ???? ?????.

???? ??? ??? ? [[BoundTargetFunction]]? ?? ??? [[Call]]? ?????. [[Call]] ? Call(boundThis, args)? ?? ??? ????. ? ?, boundThis? [[BoundThis]]??, args? ??? ??? ? ???? ???? [[BoundArguments]] ???.

???? ??? new ???? ???? ??? ?? ????: ??? ?? ?? ??? ?? ?? ??? ??? ?????. ??? this ?? ?????, ?? ??(prepend) ??? ?????? ??? ?????.

??

???? ?? ??

bind()? ?? ??? ???? ?? ??? ???? ?? this ??? ???? ??? ??? ???. ?? JavaScript ??????? ?? ??? ????? ???? ??? ? ? ??? ????, ?? ??? ? ??? this? ??? ??? ???? ???(?? : ?? ?? ???? ?? ??? ??). ??? ??? ??? ???, ???? ?? ?? ??? ?????. ?? ??? ??? ?? ??? ????, ??? ??? ???? ??? ? ????.

js
this.x = 9;
var module = {
  x: 81,
  getX: function () {
    return this.x;
  },
};

module.getX(); // 81

var retrieveX = module.getX;
retrieveX();
// 9 ?? - ??? ?? ????? ????

// module? ???? 'this'? ?? ??? ?? ??
// ?? ?????? ?? ?? x?
// module? ?? x? ??? ? ??
var boundGetX = retrieveX.bind(module);
boundGetX(); // 81

?? ?? ??

bind()? ???? ??? ???? ?? ??? ?? ??? ?? ??? ??? ???. ??? ?? ??? ??? ??? this ?? ???, ??? ? ??? ???? ??? ? ??? ??? ??? ?? ??? ?? ?? ?????.

js
function list() {
  return Array.prototype.slice.call(arguments);
}

var list1 = list(1, 2, 3); // [1, 2, 3]

// ??? ??? ???? ??? ?????.
var leadingThirtysevenList = list.bind(null, 37);

var list2 = leadingThirtysevenList(); // [37]

var list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]

function addArguments(arg1, arg2) {
  return arg1 + arg2;
}

var result1 = addArguments(1, 2); // 3

// ? ?? ??? ???? ??? ?????.
var addThirtySeven = addArguments.bind(null, 37);

var result2 = addThirtySeven(5); // 37 + 5 = 42

// ? ?? ??? ?????.
var result3 = addThirtySeven(5, 10); // 37 + 5 = 42

setTimeout? ?? ??

window.setTimeout() ??? ????, this ???? window (?? global) ??? ?????. ??? ????? ???? this? ??? ?? ??? ???? ???? ??, ???? this? ?? ??? ???? ? ????, ????? ???? ??.

js
function LateBloomer() {
  this.petalCount = Math.ceil(Math.random() * 12) + 1;
}

// 1? ?? ? bloom ??
LateBloomer.prototype.bloom = function () {
  window.setTimeout(this.declare.bind(this), 1000);
};

LateBloomer.prototype.declare = function () {
  console.log("I am a beautiful flower with " + this.petalCount + " petals!");
};

var flower = new LateBloomer();
flower.bloom();
// 1? ?, 'declare' ??? ??

???? ??? ???? ??

?? : ? ??? JavaScript ??? ??? bind() ???? ?? ?? ??(edge case)? ?????. ?? ??? ???? ?? ?? ?? ?? ??? ??? ??? ?? ???? ?? ???? ?? ???.

???? ??? ???? ?? ??? ?? ???? ??? ????? ???? new ???? ?? ??? ?????. ???? ??? ?? ???? ? ??? ??, ??? this? ?????. ???, ??? ??? ??? ??? ??? (???) ?? ????:

js
function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return this.x + "," + this.y;
};

var p = new Point(1, 2);
p.toString(); // '1,2'

// ?? ?????? ???? ??,

// ? bind?? ? ??:

var YAxisPoint = Point.bind(null, 0 /*x*/);

var emptyObj = {};
var YAxisPoint = Point.bind(emptyObj, 0 /*x*/);

var axisPoint = new YAxisPoint(5);
axisPoint.toString(); // '0,5'

axisPoint instanceof Point; // true
axisPoint instanceof YAxisPoint; // true
new Point(17, 42) instanceof YAxisPoint; // true

new? ?? ?? ?? ???? ??? ??? ?? ??? ?? ? ??? ??? ?????. ? ?? ??? ???? ???? ??? ??? ?? ??? ???? ? ??? ????, ??? new? ????? ???? ???? ??? ???? ????.

js
// ?? JavaScript ???? ?? ??? ? ??
// ...????? ???

// ??? ?? ???? ??? ? ??
// (?? ?? ??? ????)
YAxisPoint(13);

emptyObj.x + "," + emptyObj.y;
// >  '0,13'

??? new? ????? ????? ???? ??? ??? ???? ?? ??, ?? ??? ? ??? ???? ???.

?? ?? ??

bind()? ?? this ?? ??? ?? ??? ?? ??(shortcut)? ??? ?? ???? ??? ???.

??, ?? ?? ??? ?? ??? ???? ? ???? ?? Array.prototype.slice? ????. ?? ?? ?? ??? ?? ? ????:

js
var slice = Array.prototype.slice;

// ...

slice.apply(arguments);

bind()?, ?? ???? ? ????. ?? ?? ????, slice? Function.prototype? apply() ??? ???? ?????, this ?? Array.prototype? slice() ??? ??? ?. ?? ?? apply() ??? ??? ? ??? ????:

js
// ?? ??? "slice"? ??
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.apply.bind(unboundSlice);

// ...

slice(arguments);

???

bind ??? ECMA-262 ?5?? ???????; ????? ?? ????? ?? ? ????. ???? ?? ??? ?? ??? ?????? ? ??? ????? ???? ???, bind() ???? ?? ????? ???? ??? ??? ? ????.

js
if (!Function.prototype.bind) {
  Function.prototype.bind = function (oThis) {
    if (typeof this !== "function") {
      // ECMAScript 5 ?? IsCallable ???
      // ??? ?? ??? ?
      throw new TypeError(
        "Function.prototype.bind - what is trying to be bound is not callable",
      );
    }

    var aArgs = Array.prototype.slice.call(arguments, 1),
      fToBind = this,
      fNOP = function () {},
      fBound = function () {
        return fToBind.apply(
          this instanceof fNOP ? this : oThis,
          aArgs.concat(Array.prototype.slice.call(arguments)),
        );
      };

    if (this.prototype) {
      // Function.prototype? prototype ??? ??
      fNOP.prototype = this.prototype;
    }
    fBound.prototype = new fNOP();

    return fBound;
  };
}

? ????? ???? ???? ? ?? ??(??? ?? ??? ?? ? ????, ? ??? ?? ??? ?? ????) ? ??? ?????:

? ?? ??? ??? ?? ??, ??? ECMA-262 ?5?? ??? ??? ???? ??? ???! ??? ?? ??(? ??? ?? ??? ??? ?? ?? ??)??, ? ?? ??? bind()? ??? ?? ?? ??? ??? ??? ??? ? ? ????.

???

Specification
ECMAScript? 2026 Language?Specification
# sec-function.prototype.bind

???? ???

??