Object-Oriented Fundamentals

Source: Internet
Author: User

Create an object, and then give the object new properties and methods.

var New Object ();    // create an object box.name = "Zhang San";    // Create a property of name and assign a value of box.age = "a";    // Create a property of age and assign a value box.run=function() {    // Create a Run () method and return a value    Returnthis. name+ "::" +this. Age}alert (Box.run ());  // Output The value of a property or method  

The

Creates an object above and creates properties and methods, and this in the Run () method represents the box object itself. This kind of thing JS create the object the most basic method, but there is a disadvantage, want to create a similar object, will produce a lot of code.

 var  Box2=box; //    get the box reference  box2.name= "John Doe"; //     alert (Box2.run ()); //  found name also changed  var  box2 = new   =" John Doe ";box2.age  = 100 ;box2.run  = function   () { return  this . Name+this  .age;}        Alert (Box2.run ());  //  This avoids confusion with box, thus keeping it separate  

To solve the problem of several similar object declarations, we can use a method called Factory mode, which is to solve the problem that the instantiation object produces a lot of duplication.

functionMyObject (name, age) {varobj =NewObject (); Obj.name=Name;obj.age=Age;obj.run=function () {return  This. Name + This. Age;};returnobj;}varBox1 = MyObject (' Zhang San ', 20); varBox2 = MyObject (' John Doe ', 100alert (Box1.run ()); alert (Box2.run ()); //remain independent

Factory mode solves the problem of repeated instantiation, but there is a problem, that is the problem of identification, because there is no way to figure out exactly which object they are an instance of.

JS can use a constructor method to wear a particular object, similar to object objects.

function Box (name,age) {    // constructor mode this  . name = name;   this. Age = Age ;    This function () {     returnthis. name+the. Age;  }} var New Box ("Zhang San");    // new Box () var New Box ("John Doe", 100);

Alert (Box1.run ());

Alert (box1 instanceof Box); ture can be clearly identified as belonging to box

Using the method of enough functions, that is, solve the problem of duplication, but also solve the problem of object recognition but the problem is, there is no new object (), why can I instantiate box (), where does this come from?

Methods that use constructors, and methods that use Factory mode, they differ as follows:
1. The constructor method does not display the creation object (new object ());
2. Assign properties and methods directly to the This object;
3. There is no Renturn statement.

Constructor methods have some specifications:
1. The function name and instantiation construct name are the same and uppercase, (PS: not mandatory, but it helps to distinguish between constructors and
normal function);
2. Create an object from a constructor, you must use the new operator.

Now that you can create an object from a constructor, where does this object come from, where is new object ()
Executed? The following procedures are performed:
1. When the constructor is used and the new constructor (), the new Object () is executed in the background;
2. The scope of the constructor is given to the new object (that is, the object created by new object), and this in the function body
Represents the object that is a new object ().
3. Execute the code within the constructor;
4. Returns the new object (returned directly from the background).
For the use of this, this is actually a reference to the current scope object. If this is the global scope, the
The Table window object, if in the constructor body, represents the object declared by the current constructor.

The only difference between constructors and normal functions is that they are called in different ways. However, the constructor is also a function
Number, it must be called with the new operator, otherwise it is a normal function.

var New // Constructed mode call alert (Box.run ()); Box (// Normal mode call, invalid varnew// object impersonating call alert ( O.run ());

Object-Oriented Fundamentals

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.