| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Function/bind | [Back] [Original] |
Get to know MDN better
const module = {
x: 42,
getX() {
return this.x;
},
};
const unboundGetX = module.getX;
console.log(unboundGetX()); //
// : undefined
const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// : 42
bind(thisArg)
bind(thisArg, arg1)
bind(thisArg, arg1, arg2)
bind(thisArg, arg1, arg2, /* , */ argN)
thisArgarg1, , argN func
this
bind() (bound function) (target function) this const boundFn = fn.bind(thisArg, arg1, arg2) const boundFn = (...restArgs) => fn.call(thisArg, arg1, arg2, ...restArgs) boundFn
boundFn.bind(thisArg, /* */) boundFn2 boundFn2 boundFn this boundFn2 boundFn fn fn boundFn boundFn2 boundFn2
"use strict"; // `this`
function log(...args) {
console.log(this, ...args);
}
const boundLog = log.bind("this value", 1, 2);
const boundLog2 = boundLog.bind("new this value", 3, 4);
boundLog2(5, 6); // "this value", 1, 2, 3, 4, 5, 6
new this Reflect.construct this new.target new.target
class Base {
constructor(...args) {
console.log(new.target === Base);
console.log(args);
}
}
const BoundBase = Base.bind(null, 1, 2);
new BoundBase(3, 4); // true, [1, 2, 3, 4]
class Derived extends class {}.bind(null) {}
// TypeError: Class extends value does not have valid prototype property undefined
instanceof instanceof prototype
class Base {}
const BoundBase = Base.bind(null, 1, 2);
console.log(new Base() instanceof BoundBase); // true
bind() this
JavaScript this
bind()
// 'this' 'globalThis'
this.x = 9;
const module = {
x: 81,
getX() {
return this.x;
},
};
// 'getX' 'this' 'module'
console.log(module.getX()); // 81
const retrieveX = module.getX;
// 'retrieveX' 'this' 'globalThis'
console.log(retrieveX()); // 9
// 'boundGetX' 'this' 'module'
const boundGetX = retrieveX.bind(module);
console.log(boundGetX()); // 81
:
retrieveX this globalThis undefined retrieveX()
ECMAScript this globalThis undefined this.x = 9
Node CommonJS this globalThis module.exports this globalThis undefined retrieveX() undefined this.x = 9 getX (globalThis) (module.exports)
Intl.NumberFormat.prototype.format()
bind()
this
function list(...args) {
return args;
}
function addArguments(arg1, arg2) {
return arg1 + arg2;
}
console.log(list(1, 2, 3)); // [1, 2, 3]
console.log(addArguments(1, 2)); // 3
//
const leadingThirtySevenList = list.bind(null, 37);
//
const addThirtySeven = addArguments.bind(null, 37);
console.log(leadingThirtySevenList()); // [37]
console.log(leadingThirtySevenList(1, 2, 3)); // [37, 1, 2, 3]
console.log(addThirtySeven(5)); // 42
console.log(addThirtySeven(5, 10)); // 42
// ( 10 )
setTimeout() this globalThis window this this
class LateBloomer {
constructor() {
this.petalCount = Math.floor(Math.random() * 12) + 1;
}
bloom() {
// 1
setTimeout(this.declare.bind(this), 1000);
}
declare() {
console.log(` ${this.petalCount} `);
}
}
const flower = new LateBloomer();
flower.bloom();
// After 1 second, calls 'flower.declare()'
class LateBloomer {
bloom() {
// 1
setTimeout(() => this.declare(), 1000);
}
}
new this
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.toString = function () {
return `${this.x},${this.y}`;
};
const p = new Point(1, 2);
p.toString();
// '1,2'
// thisArg
const YAxisPoint = Point.bind(null, 0 /* x */);
const axisPoint = new YAxisPoint(5);
axisPoint.toString(); // '0,5'
axisPoint instanceof Point; // true
axisPoint instanceof YAxisPoint; // true
new YAxisPoint(17, 42) instanceof Point; // true
newnew.target, instanceof, this extends
new new this
const emptyObj = {};
const YAxisPoint = Point.bind(emptyObj, 0 /* x */);
//
// ()
YAxisPoint(13);
// `this`
console.log(emptyObj); // { x: 0, y: 13 }
new new new.target !== undefined
bind()
class Base {
static baseProp = "base";
}
class Derived extends Base {
static derivedProp = "derived";
}
const BoundDerived = Derived.bind(null);
console.log(BoundDerived.baseProp); // "base"
console.log(BoundDerived.derivedProp); // undefined
console.log(new BoundDerived() instanceof Derived); // true
bind() this this array.map(callback) map(array, callback) arguments Object.prototype map
const slice = Array.prototype.slice;
//
slice.call(arguments);
slice.call call() this bind() call() this slice() Function.prototype.call() this Array.prototype.slice() call()
// "slice"
const unboundSlice = Array.prototype.slice;
const slice = Function.prototype.call.bind(unboundSlice);
//
slice(arguments);
| ECMAScript 2027 LanguageSpecification # sec-function.prototype.bind |
| Web Proxy Viewer | New URL | Original Page |