Javascript, there are two kinds of development modes:
1. Functional (procedural) 2. In the face of objects (OOP), there is a flag in the face of object language, that is, a class, and through a class you can create any number of properties and methods, and ECMAScript does not have the concept of a class, so its objects are also different from those in class-based languages.
1. Create an Object
Create an object, and then give the object new properties and methods.
Var box=new object ();//Create Objects box.name= ' link '; Add attribute, value box.age=45;box.run=function () {//Create method return this.name+this.age+ ' run .... ';} Alert (Box.run ());
The above creates an object and gives the object new properties and methods. The This in the Run () method represents the Box object itself, which is the most basic method of JS object creation, but one drawback is to create a
Similar objects, a lot of code will be generated.
Var box=new object ();//Create Objects box.name= ' link '; Add attribute, value box.age=45;box.run=function () {//Create method return this.name+this.age+ ' run .... ';} Alert (Box.run ()); var box1=new Object (); box1.name= ' HTML '; Box1.age=45;box1.run=function () {return this.name+this.age + ' running .... ';} Alert (Box1.run ());
In order to solve the problem of multiple object declarations, let me briefly describe the method of Factory mode, which is to solve the large number of duplication problems caused by instantiated objects.
function CreateObject (name,age) {var obj=new Object (); Create object Obj.name=name; Add Property Obj.age=age;obj.run=function () { //Create method return this.name+this.age+ "Running ..."}return obj; Returns the object}var box=createobject (' link '); Create the first object var box1=createobject (' Lxl ',%) //Create a second object alert (Box.run ()); alert (Box1.run ());
It solves a lot of repetitive problems, but there is one problem that is identified, because there is no way to figure out exactly which object they are.
I will write the next article.
PS: Of course, my understanding, may not be all right, please small friends, many points, many advice!
JavaScript faces the object. First article