클래스 var Emp = function() { };
를 만든 다음 클래스의 프로토 타입에서 함수를 다음과 같이 정의합니다.
Emp.prototype.function1 = function() { /* some definition */ };
Emp.prototype.function2 = function() { /* some definition */ };
// .. so on
성능에 어떤 영향을 미치며 어떤 접근 방식이 효과적이며 그 이유는 무엇입니까?
사례 1 : 함수를 호출하면
Emp.prototype.function1();
사례 2 : 처음으로 Emp
인스턴스를 생성하는 경우
var empobj = new Emp();
empobj.function1(); // function calling
사례 1을 사용하면 this
컨텍스트가 prototype
기본적으로 개체를 가리 키 므로이를 사용하여 instance
값에 액세스 할 수 없습니다.
예를 들면 :
var x = function(){ this.a = 10 };
x.prototype.y = function(){ console.log(this.a) };
x.prototype.y(); // This will print undefined since
//there is no properties other than y in the prototype of x.
동시에 경우 2, 값 prototype
뿐만 아니라 개체에 액세스 할 수 있습니다 instances
. 이에 대한 샘플 코드는 다음과 같습니다.
var x = function(){ this.a = 10 };
x.prototype.y = function(){ console.log(this.a) };
var obj = new x();
obj.y(); // `y` can be accessed also. And it will output 10.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다