1. Simple packaging
function Cat(name,color){ this.name = name; this.color = color; } Cat.prototype.type = "Cat"; Cat.prototype.eat = function(){alert("eat mice")};
Use the this variable inside a function and bind it to the instance object; Prototype objects are defined by those invariant properties and methods.
2. Inheritance of constructors
Cat.prototype = new Animal(); Cat.prototype.constructor = Cat; var cat1 = new Cat("big hair", "yellow"); alert(cat1.species); // animal
Line 1: Point the prototype object of Cat to an instance of Animal
The second line: (any prototype object has a constructor property) Cat.prototype.constructor originally pointed to Cat, and now points to Animal.
3. Inheritance of non-constructor functions
For example: how to make "doctor" inherit "Chinese"
var Doctor = object(Chinese); Doctor.career = 'Doctor'; alert(Doctor.nation); //China
First, create a child object on the parent object. Then, add the child object's own properties. Finally, the child object inherits the properties of the parent object.
Only some simple methods are described here, so that people can roughly understand what object-oriented does. There are some deeper content, if you are interested, you can refer to http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html for access.