jquery Storm Tutorial-javascript prototype __java

Source: Internet
Author: User
Tags class definition instance method

Http://uefirst.com/bbs/simple/?t240.html

The prototype in JavaScript

The prototype in JavaScript (Prototype) is one of the most special places in JavaScript. Whether it's object-oriented or inherited JavaScript, using prototype is essential.
1 using prototypes to implement JavaScript-oriented objects
"Stereotype" represents the original state of an object, and each object in JavaScript has a prototype attribute, but only the prototype property of a function type can be manipulated directly by using a script. The prototype of object belongs to internal properties and cannot be directly manipulated. The prototype property itself is an object type. All of the defined properties and methods on the prototype of a function exist on its instance object. So prototype is the instance method and instance property in the C # class. Instance methods are different from static methods, which are methods that you do not need to declare an instance of a class, and an instance method refers to a method that must declare an instance of a class using the "new" keyword before it can be accessed through this instance.
The following are the ways in which two methods are declared:

function Staticclass () {}; Declares a class
Staticclass.staticmethod = function () {alert ("static Method")};//create a static approach
StaticClass.prototype.instanceMethod = function () {"Instance method"}; Create an instance method

The above first declares a class Staticclass, then adds a static method Staticmethod and a dynamic method Instancemethod, except that the prototype prototype property is used to add dynamic methods.
For static methods, you can call directly:

Staticclass.staticmethod ();

However, instance methods cannot be invoked directly:

Staticclass.instancemethod (); Statement error, unable to run.

An instance method needs to be instantiated before it can be invoked:

var instance = new Staticclass ();//First Instantiate
Instance.instancemethod (); Instance method can be invoked on an instance

You can also declare instance properties by using prototype in addition to declaring an instance method. Because the prototype has such a powerful function, it is possible to use prototypes to implement JavaScript-oriented objects. There are many ways to create objects in object-oriented form, this book is not explained in detail, only one of the most common, easiest to understand and use the method: Dynamic prototyping method.
Suppose you want to define an automobile car type, with property color and Method Showcolor (), you can declare and use the following ways:

function car ()
{
          //DECLARE attribute
    This.color = ' none ';
          Declaration Method
    if (typeof car._initialized = = "undefined")
    {
    Car.prototype.showColor = function ()
    {
        alert (This.color);
    }
    }
    Car._initialized = true;
var car = new car ();
Car.showcolor (); The output is "none"
Car.color = "Blue";
Car.showcolor (); Output is "blue"

The essence of a dynamic prototyping approach is to declare an instance method using prototype, which declares the instance property. In addition to being closer to object-oriented programming, this approach is characterized by simplicity. Note the function that acts as a "class definition" does not have any arguments, although it can also pass arguments in, but do not do so. This is the preparation for the following section to implement the inheritance mechanism using the prototype chain.
You can assign a value to a property after the object is created. It's a bit of a hassle, but the code is simple and easy to understand.
2 using the prototype chain to implement inheritance
In addition to object-oriented declarations, object inheritance is most commonly used in an object-oriented world. You can use prototype to implement object inheritance in JavaScript. Continue with the example of the car in the previous section. Suppose the car has two derived classes, one is Goodcar, and is characterized by fast running. One is Badcar, which is characterized by slow running. But they have the color attribute and the Showcolor method as well as the car.
Take the Goodcar class, for example, to implement type inheritance as long as the Goodcar prototype property is an instance of the car class:

Goodcar.prototype = new car ();

Now, the Goodcar class already has all the properties and methods of car:

var goodcar = new Goodcar ();
Goodcar.showcolor (); Output  "None"

After the inheritance is implemented, Goodcar also implements its own run () method, also using the prototype implementation, and the following is the complete definition of the Goodcard class:

Create Goodcar class
function Goodcar ()
{        }
goodcar.prototype = new Car ();
GoodCar.prototype.run = function () {alert ("Run Fast");}

It is important to note that the method of Goodcar class itself must be defined after the implementation of the inheritance statement.
Why do you call this approach a prototype chain inheritance? Because the prototype of Goodcar is Car,car prototype is object, which means that Goodcar also has all the properties and methods of object objects. This is a "chain" of the structure.
Using prototypes, you can quickly and easily implement JavaScript object-oriented and inheritance.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.