4

このプロトタイプについて理解したい。

で使用されているプロトタイプの違いは何ですか?

https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScriptリンクおよび http://www.prototypejs.org/learn/class-inheritance

前もって感謝します。

4

3 に答える 3

2

There is no difference. Prototype JS is just a framework that makes working with JavaScript easier. The prototype property in both cases belong to functions which are being used as constructors, which is simply JavaScript.

If you would like to know more about inheritance in JavaScript then read the following answer. Personally I don't like using any framework. The only framework I use is Vapor.js. However when working with classes I usually make use of the following gist:

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;
    }
}();

This allows me to create classes as follows:

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;
});

Inheritance is as simple as:

var Square = new Class(function (uber) {
    return function (side) {
        uber(side, side);
    };
}, Rectangle);

You may also use base class methods like:

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);

Each class has it's own prototype object. Thus any properties on the prototype or the class itself (static properties) are automatically inherited by each derived class. Properties defined inside the class will not be inherited until the uber function is called. The uber function returns an instance of the base class so that the base class methods may be called.

Edit: Here's a working example of the above pattern:

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
于 2012-07-24T04:17:45.697 に答える
1

2 番目のリンクは、JavaScript アプリケーションを構築するためのフレームワークであるPrototypeについて説明しています。固有名詞です。最初のリンクで使用されている「プロトタイプ」という言葉は、JavaScript が内部でどのように機能するかを表す標準的な用語です。フレームワークは、プロトタイプのコンセプトにちなんで名付けられました。

JavaScript の「プロトタイプ」の概念については、このリンクに非常によく紹介されています。

于 2012-07-24T04:04:43.677 に答える
0

https://developer.mozilla.org/en/Introduction_to_Objectで言及されているプロトタイプ-Oriented_JavaScriptはプログラミングスタイルです(を使用しないオブジェクト指向プログラミングclass)。一方

http://www.prototypejs.orgで言及されているプロトタイプは、jQueryやMooToolsなどに似たJavaScriptライブラリまたはフレームワークです。

于 2012-07-24T04:05:06.527 に答える