이 프로토 타입에 대해 알고 싶습니다. 프로토 타입 및 프로토 타입 정보
당신의이https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript 링크 와 사전에 http://www.prototypejs.org/learn/class-inheritance
덕분에 사용 된 프로토 타입의 차이점이 무엇인지 명확히 할 수 있습니다.
이 프로토 타입에 대해 알고 싶습니다. 프로토 타입 및 프로토 타입 정보
당신의이https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript 링크 와 사전에 http://www.prototypejs.org/learn/class-inheritance
덕분에 사용 된 프로토 타입의 차이점이 무엇인지 명확히 할 수 있습니다.
두 번째 링크는 프로토 타입 (JavaScript 응용 프로그램을 작성하기위한 프레임 워크)을 설명합니다. 그것은 적절한 이름입니다. 임의의 예제를 선택하는 것과 같은 종류의 윈도우 시스템 이름 지정과 같은 종류입니다. 첫 번째 링크에서 사용되는 "프로토 타입"이라는 단어는 JavaScript가 내부적으로 작동하는 방식에 대한 표준 용어입니다. 프레임 워크는 프로토 타입 개념을 따서 명명되었습니다.
JavaScript의 this link에있는 "프로토 타입"개념에 대해 아주 잘 소개되어 있습니다.
https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript에 언급 된 프로토 타입은 프로그래밍 스타일입니다 (class
을 사용하지 않는 객체 지향 프로그래밍). 반면
http://www.prototypejs.org에서 언급 된 프로토 타입은 jQuery, MooTools 등과 비슷한 JavaScript 라이브러리 또는 프레임 워크입니다.
차이는 없습니다. 프로토 타입 JS는 JavaScript로 작업하기가 더 쉬운 프레임 워크 일뿐입니다. 두 경우 모두 prototype
속성은 생성자로 사용되는 함수에 속하며 이는 단순히 JavaScript입니다.
JavaScript에서 상속에 대해 더 자세히 알고 싶다면 answer을 읽어보십시오. 개인적으로 어떤 프레임 워크를 사용하길 좋아하지 않습니다. 내가 사용하는 유일한 프레임 워크는 Vapor.js입니다.
var Square = new Class(function (uber) {
return function (side) {
uber(side, side);
};
}, Rectangle);
:
var Rectangle = new Class(function() {
var width;
var height;
function constructor(length, breadth) {
width = length;
height = breadth;
}
this.area = function() {
return width * height;
};
return constructor;
});
상속과 같이 간단하다 :
var Class = function() {
var slice = Array.prototype.slice;
var bind = Function.prototype.bind;
var prototype = Class.prototype = new Function;
return prototype.constructor = Class;
function Class(definition, base) {
var klass = function() {
var instance = this;
if (base instanceof Class)
var uber = function() {
if (uber instanceof base) return uber;
arguments = slice.call(arguments);
arguments = [null].concat(arguments);
uber = bind.apply(base, arguments);
uber = new uber;
var hyper = instance.__proto__ = uber;
var proto = hyper.__proto__;
while (proto != parent) {
hyper = proto;
proto = hyper.__proto__;
}
hyper.__proto__ = child;
return uber;
};
var constructor = definition.call(this, uber);
constructor.apply(this, arguments);
};
if (base instanceof Class) {
klass.__proto__ = base;
var child = klass.prototype;
var parent = child.__proto__ = base.prototype;
} else klass.__proto__ = prototype;
return klass;
}
}();
이 다음과 같이 나를 클래스를 만들 수 있습니다 : 클래스를 사용하여 작업 할 때 그러나 나는 일반적으로 다음과 gist를 사용합니다 다음과 같은 기본 클래스 메소드를 사용할 수도 있습니다.
var Cube = new Class(function (uber) {
var side;
function constructor() {
side = arguments[0];
uber = uber(side);
}
this.area = function() {
return 6 * uber.area();
};
this.volume = function() {
return side * uber.area();
};
return constructor;
}, Square);
각 클래스에는 고유 한 프로토 타입 객체가 있습니다. 따라서 프로토 타입이나 클래스 자체의 모든 속성 (정적 속성)은 파생 클래스별로 자동으로 상속됩니다. 클래스 내에 정의 된 속성은 uber
함수가 호출 될 때까지 상속되지 않습니다. uber
함수는 기본 클래스의 메서드를 호출 할 수 있도록 기본 클래스의 인스턴스를 반환합니다. 편집
: 여기 위의 패턴의 working example입니다 :
var cube = new Cube(5);
alert("Side of the cube: 5");
alert("Area of the cube: " + cube.area()); // 150
alert("Volume of the cube: " + cube.volume()); // 125