JavaScript learning note--8. Object oriented

Source: Internet
Author: User

1. Encapsulation--Initialization mode

var Cat = {
Name: ' Jack ',
Age:5,
Say:function () {
Alert (' Hello world! ');
}
}

Or:

var Cat = {}
Cat.name = ' Jack ';
Cat.age = 5;
Cat.say = function () {
Alert (' Hello world! ');
}

alert (cat.name);
Cat.say ();

2. Package--object mode

function Cat (name,age) {
var c = new Object ();
C.name = name;
C.age = age;
return C;
}

var cat1 = Cat (' Jack ', 5);

alert (cat1.name);

3. Encapsulation--constructor function mode

function Cat (name,age) {
THIS.name = name;
This.age = age;
}
Cat.prototype.say = function () {//Use prototype to point to the same memory address for the same attribute/method in multiple instances
Alert (' Hello world! ');
}

var cat1 = new Cat (' Jack ', 5);

alert (cat1.name);
Cat1.say ();

4. Traverse

for (var c in cat1) {
alert (c);//Popup All properties of the object
Alert (cat1[c]);//The value of all properties of the Popup object
}

5. Inheriting--apply mode

function Animal () {
this.species = ' animals ';
}
function Cat (name) {
Animal.apply (this, arguments);
THIS.name = name;
}

var cat1 = new Cat (' Jack ');

alert (cat1.species);

6. Inheriting--prototype mode

function Animal () {
this.species = ' animals ';
}
function Cat (name) {
THIS.name = name;
}
Cat.prototype = new Animal ();//The prototype of cat is pointed to Animal
Cat.prototype.constructor = cat;//corrects an instance of cat to inherit cat

var cat1 = new Cat (' Jack ');

alert (cat1.species);

7. polymorphic

Define abstract methods in a class (format the method only, do not do any specific implementation)

A class with an abstract method becomes an abstract class that does not derive any instances and is used only for the Quilt class inheritance

After the subclass inherits, according to own demand, then carries on the concrete implementation to the original parent class abstract method

This process is called "polymorphism."

@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.

JavaScript learning note--8. Object oriented

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.