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