1
내가 생성자에 대한 Resig 씨의 makeClass() 방식 사용이 코드에서자바 스크립트 : 중첩 된 (내부) 유형의 기본 디자인은 무엇입니까?
// makeClass - By John Resig (MIT Licensed)
// Allows either new User() or User() to be employed for construction.
function makeClass(){
return function(args){
if (this instanceof arguments.callee) {
if (typeof this.init == "function")
this.init.apply(this, (args && args.callee) ? args : arguments);
} else
return new arguments.callee(arguments);
};
}
// usage:
// ------
// class implementer:
// var MyType = makeClass();
// MyType.prototype.init = function(a,b,c) {/* ... */};
// ------
// class user:
// var instance = new MyType("cats", 17, "September");
// -or-
// var instance = MyType("cats", 17, "September");
//
var MyType = makeClass();
MyType.prototype.init = function(a,b,c) {
say("MyType init: hello");
};
MyType.prototype.Method1 = function() {
say("MyType.Method1: hello");
};
MyType.prototype.Subtype1 = makeClass();
MyType.prototype.Subtype1.prototype.init = function(name) {
say("MyType.Subtype1.init: (" + name + ")");
}
을합니다 MyType()는 최상위 유형이며, MyType.Subtype1는 중첩 된 유형입니다.
를 사용하려면, 내가 할 수있는 :
var x = new MyType();
x.Method1();
var y = new x.Subtype1("y");
내가 (Subtype1의 초기화() 내에서, 부모 유형의 인스턴스에 대한 참조를받을 수)? 어떻게?
노트, 이것은이 완벽하게 작동, – Tyson
감사합니다 단순한 1 분 예입니다. – Cheeso