this question과 유사하게 Douglas Crockford의 자바 스크립트, 좋은 부품을 따르고 있습니다. 4 장에서, 그는 매우 혼란 스럽다. 그는 다음 새 integer
방법 테스트이 JavaScript 확장 방법은 어떻게 작동합니까?
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Number.method('integer', function () {
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
: 그는이 샘플 코드 기록
document.writeln((-10/3).integer()); // -3
내가 this.prototype[name]
의 구문을 이해하지를 (특히, 괄호) 또는 Math[this < 0 ? 'ceiling' : 'floor'](this)
이 (다시, 대괄호 및 Math
이 어디서 왔는지). 더 중요한 것은 누군가가 코드가 일반적으로 어떻게 작동하는지, 테스트 코드가 작동하는 이유를 설명 할 수 있습니까?
'obj.foo;''obj [ "foo"];'var foo = "foo"; obj [foo];'모두 같은 결과를줍니다. –