Object-oriented language has a flag, that is, the concept of owning classes, abstract instance objects of public properties and methods, based on the class can create any number of instance objects, generally with encapsulation, inheritance, polymorphism characteristics! But the object in JS is different from the object in pure object-oriented language, the ECMA standard defines the object in JS: A collection of unordered attributes, whose properties can contain basic values, objects, or functions. The object that can be easily understood as JS is a set of unordered values in which the property or method has a name that can access the mapped value (the value can be the base value/object/method).
1, the JSON format object-oriented, a namespace, suitable for modular development plug-in. Different modules use different namespaces. can also be used for the development of small projects.
var person = { ' My name ', +, function() { return This . Name;} }
2. Combinatorial constructors and prototype patterns
The most commonly used method of defining type is the combination of constructor pattern and prototype pattern. The constructor pattern is used to define the properties of the instance, whereas the prototype pattern is used to define methods and shared properties. As a result, each instance will have its own copy of the instance properties, but at the same time share a reference to the other method, maximizing memory savings. In addition, the combination mode supports passing parameters to the constructor, which is the director of the two family.
functionPerson (name, age, job) { This. Name =name; This. Age =Age ; This. Job =job; This. lessons = [' Math ', ' Physics '];} Person.prototype={Constructor:person,//The prototype literal method changes the object's constructor into object and also forces the person to refer back to the personGetName:function () { return This. Name; }}varPerson1 =NewPerson (' Jack ', ' software engneer ');p Erson1.lessons.push (' Biology ');varPerson2 =NewPerson (' Lily ', mechanical, ' Engneer '); alert (person1.lessons);//math,physics,biologyalert (person2.lessons);//Math,physicsAlert (Person1.getname = = = Person2.getname);//true,//defining methods in shared prototypes
The disadvantage of the prototype pattern is that if the property value is a reference type, it will result in modifying this property of an object, and this property of the other object is changed.
The disadvantage of the constructor pattern is that each instance of the same method has, instead of a shared method. Not optimized.
Inherited
Inheritance of attributes:
function Worker (name,age,job) { Person.call (this, name,age); // the inheritance of the property. this . Job = job;}
Inheritance of methods:
Worker.prototype = {}; for (var in person.prototype) { =function() { Alert (this. Job)}
Full inheritance:
functionPerson (name,age) { This. Name =name; This. Age =Age ; This. Friends = [' Shelby ', ' Court '];} Person.prototype={sayname:function() {alert ( This. Name); }}functionWorker (name,age,job) {Person.call ( This, name,age);//the inheritance of the property. This. Job =Job;} Worker.prototype= {}; for(varIinchPerson.prototype) {Worker.prototype[i]=person.prototype[i];} Worker.prototype.sayJob=function() {alert ( This. Job)}varW =NewWorker (' Li ', 12, ' handyman ');varp =NewPerson (' AI ', 21); W.sayname (); W.sayjob ();p. Sayname ();p. Sayjob ();
Object-oriented programming in JS