Node.js 바인드 함수는 클로저를 사용하여 실행 context_javascript 팁을 저장합니다.
May 16, 2016 pm 05:57 PM
bind
코드 복사 코드는 다음과 같습니다.
window.name = "the window object"
function rangeTest( ) {
return this.name;
}
// 전역 범위에서 함수 호출:
scopeTest()
// -> >var foo = {
name: "foo 객체!",
otherScopeTest: function() { return this.name }
}
foo.otherScopeTest();// -> "foo 객체!"
var foo_otherScopeTest
foo_otherScopeTest()
// –> "창 객체"
bind 구현은 다음과 같습니다.
코드 복사 코드는 다음과 같습니다.
// Prototype.js의 .bind 메서드
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args. Shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)))
}; };
사용 예:
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args. Shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)))
}; };
사용 예:
코드 복사 코드는 다음과 같습니다.
var obj = { name: '좋은 데모',
fx: function() {
alert(this.name);
}
}; >window.name = '나는 정말 아름다운 창문이다!';
function runFx(f) {
f()
}
var fx2 = obj.fx.bind(obj);
runFx(obj.fx) ;
runFx(fx2);
참조:
http://www.prototypejs.org/api/function/bind
추신: fx: function() {
alert(this.name);
}
}; >window.name = '나는 정말 아름다운 창문이다!';
function runFx(f) {
f()
}
var fx2 = obj.fx.bind(obj);
runFx(obj.fx) ;
runFx(fx2);
참조:
http://www.prototypejs.org/api/function/bind
방금 프로토타입js를 발견했습니다. API 문서에 아주 자세하게 설명되어 있으니 시간을 내어 자세히 읽어보세요.
간단한 구현:
코드 복사 코드는 다음과 같습니다. 함수. 프로토타입 .bind = function(obj) {
var _this = this; return function() {
return _this.apply(obj,
Array.prototype.slice.call(arguments));
}
}
var name = 'window',
foo = {
name:'foo object',
show:function() {
return this.name ;
}
};
console.assert(foo.show()=='foo object',
'예상된 foo 개체, 실제는 ' foo.show()); var foo_show = foo.show;
console.assert(foo_show()=='window',
'예상되는 창, 실제는 ' foo_show())
var foo_show_bind = foo.show.bind ( foo);
console.assert(foo_show_bind()=='foo object',
'foo 객체가 예상되지만 실제는 ' foo_show_bind());
return _this.apply(obj,
Array.prototype.slice.call(arguments));
}
}
var name = 'window',
foo = {
name:'foo object',
show:function() {
return this.name ;
}
};
console.assert(foo.show()=='foo object',
'예상된 foo 개체, 실제는 ' foo.show()); var foo_show = foo.show;
console.assert(foo_show()=='window',
'예상되는 창, 실제는 ' foo_show())
var foo_show_bind = foo.show.bind ( foo);
console.assert(foo_show_bind()=='foo object',
'foo 객체가 예상되지만 실제는 ' foo_show_bind());
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

인기 기사
2 포인트 박물관 : Bungle Wasteland Location Guide
4 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌

핫툴 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7109
9


자바 튜토리얼
1534
14


라라벨 튜토리얼
1253
25


PHP 튜토리얼
1205
29


Cakephp 튜토리얼
1152
46

