简体   繁体   English

如何使用prototype.js创建一个类

[英]How to create a class using prototype.js

任何人都可以展示使用prototype.js创建类的示例及其工作原理。任何人都可以为prototype.js提供除官方网站以外的优秀示例和教程吗?

Creating PrototypeJS Classes is very similar to creating classes in normal OOP languages. 创建PrototypeJS Classes与在普通OOP语言中创建类非常相似。

First start off by naming your class 首先从命名你的班级开始

var myClass = Class.create({ });

This will create an empty class - now populate it with methods, if you put a method initialize PrototypeJS will fire that as the constructor 这将创建一个空类 - 现在用方法填充它,如果你把一个方法initialize PrototypeJS将会触发它作为构造函数

var myClass = Class.create(
{
    initialize : function()
    {
        this.options = 0;
    }
});

You can setup anything you want in the initialize() method like default values or just initializing the properties of the class. 您可以在initialize()方法中设置任何您想要的内容,例如默认值或只是初始化类的属性。 Lets put in some other methods and show how to instantiate the class. 让我们介绍一些其他方法,并展示如何实例化该类。

var myClass = Class.create(
{
    initialize : function()
    {
        this.options = 0;
    },

    testme : function()
    {
        this.options++;
    },

    showme : function()
    {
        alert(this.options);
        return this.options;
    }
});

var theClass = new myClass();

Lets take it one more step and call other methods within methods and pass options to the constructor. 让我们再做一步,在方法中调用其他方法,并将选项传递给构造函数。

var myClass = Class.create(
{
    initialize : function(option)
    {
        this.options = (option ? option : 0);

        this.testme();
    },

    testme : function()
    {
        this.options++;
    },

    showme : function()
    {
        alert(this.options);
        return this.options;
    }
});

var theClass = new myClass(200);
theClass.showme();

//will alert 201 and return 201

This is cool and all - but what about class inheritance? 这很酷而且全部 - 但是类继承怎么样? That is a big thing in OOP - lets say we have a separate class that is a child class of myClass . 这在OOP中是一件大事 - 假设我们有一个单独的类,它是myClass的子类。 For any method that you are overriding in the child class you can pass a first variable as $super and that will refer to the parent's method of the same name - similar to a scope resolution 对于您在子类中重写的任何方法,您可以将第一个变量作为$super传递,并且将引用同名的父方法 - 类似于范围解析

var myChildClass = Class.create(myClass,
{
    initialize : function($super,option)
    {
        $super(option);

        // the child class needs option's default value at 150 or whatever is 
        // passed so run the parent initialize first and then redefine the 
        // option property
        this.option = (option ? option : 150);

        // you can still run methods in the parent that are not overridden in 
        // the child
        this.testme();
    }
});

var child = new myChildClass();
child.showme();

//will alert() 151 and return 151

I hope this is helpful for you. 我希望这对你有所帮助。

Here are some more complex real world examples from my github 以下是我的github中一些更复杂的真实世界示例

https://github.com/jwestbrook/Prototype.Growler https://github.com/jwestbrook/Prototype.Growler

https://github.com/jwestbrook/Prototype.Watermark https://github.com/jwestbrook/Prototype.Watermark

https://github.com/jwestbrook/bootstrap-prototype https://github.com/jwestbrook/bootstrap-prototype

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

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