은 그렇게 자신을 말하는 것처럼 : 당신이 함수 객체 있습니다. 의지에 기능을 할당 할 수있는 속성과 메소드 : : 함수 그냥 객체 리터럴, 배열, 또는 다른 어떤 같은 JS의 객체이다이 경우
var someAnonFunction = function(foo)
{
console.log(this);
console.log(this === someAnonFunction);//will be false most of the time
};
someAnonFunction.x = 123;//assign property
someAnonFunction.y = 312;
someAnonFunction.divide = function()
{
console.log(this === someAnonFunction);//will be true most of the time
return this.x/this.y;//divide properties x & y
};
someAnonFunction.divide();
, someAnonFunction
에 의해 참조 함수 객체가 할당 된 익명 함수에 대한 참조는 divide
(익명 함수에 대한 참조는 어쨌든 더빙이라고 부름)이라고합니다. 따라서 여기에는 프로토 타입 참여가 없습니다. 당신이 그렇게 자신을 말하고, 당신을 마음 : 아마도이 더 분명하다,
console.log(someAnonFunction.toString === Function.prototype.toString);//functions are stringified differently than object literals
console.log(someAnonFunction.hasOwnProperty === Object.prototype.hasOwnProperty);//true
을 또는 : 모든 개체를 다시 Object.prototype
을 추적 할 수 있습니다, 그냥이 시도하는 방법/속성 호출에 해결 방법에 대한 간단한 계획을 JS의 값 :
[ F.divide ]<=========================================================\ \
F[divide] ===> JS checks instance for property divide | |
/\ || | |
|| || --> property found @instance, return value-------------------------------| |
|| || | |
|| ===========> Function.prototype.divide could not be found, check prototype | |
|| || | |
|| ||--> property found @Function.prototype, return-----------------------| |
|| || | |
|| ==========> Object.prototype.divide: not found check prototype? | |
|| || | |
|| ||--> property found @Object.prototype, return---------------------|_|
|| || |=|
|| =======>prototype is null, return "undefined.divide"~~~~~~~~~~~~~~~|X|
|| \/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~< TypeError can't read property 'x' of undefined
그러므로 당신이 프로토 타입을 사용하여 작업을 위의 코드를 원한다면, 당신은 종류의 프로토 타입을 증대해야한다는, 다음
(이 경우, Function.prototype
). 이것이 권장되지 않는다는 사실을 알고 계십시오. 실제로 "native" 프로토 타입을 자주 frown하려고합니다. 아직 : 두 경우 모두
Function.prototype.divide = function (a, b)
{
a = +(a || 0);//coerce to number, use default value
b = +(b || 1) || 1;//division by zeroe is not allowed, default to 1
return a/b;
};
function someFunction()
{
return 'someString';
};
var another = function(a, b)
{
return a + b;
};
someFunction.divide(12, 6);//will return 2
another.divide(12, 4);//3
는, 이름 (someFunction
또는 another
)에 의해 참조되는 함수 객체가 발견되지 divide
라는 속성, 검색됩니다. 그런 다음 해당 속성이있는 Function.prototype
을 검색합니다.
그렇지 않은 경우 JS는 Object.prototype
도 확인합니다. 실패하면 결국 오류가 발생합니다.
What makes my.class.js so fast? (프로토 타입 체인 거래)
Objects and functions in javascript (함수의 요점을 되풀이 <는 => < => 생성자 객체)
What are the differences between these three patterns of "class" definitions in JavaScript?을 : 나는 잠시 다시이 주제에 SO에 꽤 긴 답변을 게시 한
Javascript - Dynamically change the contents of a function (변수와 속성에 할당되고 컨텍스트가 변경된 익명의 함수에 막연히 닿아 있음)
내가 크롬에서 코드를 실행 , 그리고 그것은 말합니다 : 잡히지 않는 TypeError : 객체 함수 (num1, num2) { return num1 + num2; } '나누기'방법이 없습니다 – andri
건축상의 문제가 아닙니까? 사실 divide는 addNum의 자식 함수가 아닙니다. 오히려 그것들은 부모 클래스/객체를 가지게 될 것이며 변수와 속성을 공유하고 그것들에 대해 수학을 할 수 있을까요? –
http://doctrina.org/Javascript-Objects-Prototypes.html – testndtv