JavaScript faces the object. First article

Source: Internet
Author: User

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

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.