简体   繁体   English

关于原型和prototypejs

[英]About prototype and prototypejs

I want to understand about this prototype. 我想了解这个原型。

Can any of you clarify what is the difference between the prototype used in 你们中的任何人都可以澄清使用的原型之间的区别

https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript link and http://www.prototypejs.org/learn/class-inheritance https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript链接和http://www.prototypejs.org/learn/class-inheritance

thanks in advance. 提前致谢。

There is no difference. 没有区别。 Prototype JS is just a framework that makes working with JavaScript easier. Prototype JS只是一个使JavaScript更容易使用的框架。 The prototype property in both cases belong to functions which are being used as constructors, which is simply JavaScript. 两种情况下的prototype属性都属于用作构造函数的函数,它只是JavaScript。

If you would like to know more about inheritance in JavaScript then read the following answer . 如果您想了解有关JavaScript继承的更多信息,请阅读以下答案 Personally I don't like using any framework. 我个人不喜欢使用任何框架。 The only framework I use is Vapor.js . 我使用的唯一框架是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. 在调用uber函数之前,不会继承在类中定义的属性。 The uber function returns an instance of the base class so that the base class methods may be called. uber函数返回基类的实例,以便可以调用基类方法。

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

The second link is describing Prototype , a framework for building JavaScript applications; 第二个链接描述了Prototype ,一个用于构建JavaScript应用程序的框架; it is a proper name. 这是一个正确的名称。 (Kind of like naming a windowing system "Windows", to pick a random example.) The word "prototype" as used in the first link is the standard terminology for how JavaScript works internally. (有点像命名窗口系统“Windows”,选择一个随机的例子。)第一个链接中使用的“原型”一词是JavaScript内部工作方式的标准术语。 The framework was named after the prototype concept. 该框架以原型概念命名。

There's a very good introduction to the concept of "prototypes" in JavaScript at this link . 这个链接上有一个很好的介绍JavaScript中“原型”的概念。

Prototype mentioned at https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript is a programming style (Object-oriented programming without the use of class ); https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript中提到的原型是一种编程风格(面向对象的编程,不使用class ); whereas

Prototype mentioned at http://www.prototypejs.org is a JavaScript library or framework, similar to jQuery, MooTools and the likes. http://www.prototypejs.org中提到的Prototype是一个JavaScript库或框架,类似于jQuery,MooTools等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM