Javascrip basic (2) object

Source: Internet
Author: User
Tags format definition

 

As we mentioned in the previous article, any Object in Javascript, o instanceof Object, will return true. Therefore, at this level, I think everything in Javascript is an Object. However, Javascript is not a fully object-oriented programming language. Therefore, this discussion of simple data types does not make much sense. The objects we mentioned here mainly refer to some complex objects, including function objects.

Let's start with the most common,

Varo = newObject ()

This statement defines the most common object o, which is also the longest object definition method in object-oriented language. All objects in Javascript can be defined in this way:

Varo = newTypeDef ()

HereTypeDefRefers to the definition of the type, inJavascriptThe built-in types are defined as follows:Object,Function,Date,Array(AndBoolean, We will not discuss this simple type here)

In addition, some types also have some simple definition methods, or they can be called initialization methods. They are also initialized at the same time, for example:

Vararray1 = []; // Initialize an empty array

Vararray2 = [1, 2, 3]; // Initialize an array containing three elements: 1, 2, and 3.

 

Varjson1 = {}; // The result is the same as that of json1 = new Object ();

Varjson2 = {

Name: "json2 ",

Mytype: "object"

};

One is the array initialization method, and the other is the common Json format definition. Json2 is an object that contains two attributes: name and mytype. Of course, we can also add other members, including attribute members and behavior (function) members, to it after declaration, as follows:

Json1.newProperty = "This is json1's new property ";

Json2.newProperty = "This is json2's new property ";

Json1.newFunc = function (){

// Json1's action

};

Json2.newFunc = function (){

// Json2's action

};


In this way, a newProperty attribute and a newProperty method are added to both json1 and json2. Here we have found a way to customize complex objects, instantiate an Object, and then add attribute members and behavior members to it, or declare the Object in Json format, to initialize an object that contains attribute members and behavior members, or to mix the two methods. However, this kind of object cannot be reused because it is not a type and itself is an instance. Therefore, there is only one such instance, which in many cases cannot meet our needs, this will be discussed in subsequent chapters. Now let's continue with this object. We can use Json to define a complete one:

VarmyObj = {

Name: "my object ",

HowToDefined: "Json ",

ShowMyName: function (){

Returnthis. name;

}

}

When we use myObj. showMyName, "my object" is returned.

We can see that in the function Definition of showMyName in myObj, we introduced the this keyword, which is the most common keyword in Object-Oriented Programming and is hard to understand in Javascript. In the above example, it indicates the object myObj. In this single object, it is much easier to understand, because it represents the myObj object, so returnthis. you can also use return myObj. name. It is confusing to use this in the definition of a function (type). this will be discussed later. Let's take a small example:

FunctionmyType (){

This. name = "my type ";

}

Varo = newmyType (); // for monitoring o, you can see o. name = "my type". In this case, this indicates the object o

 

// Then we call myType () directly as a function call

MyType (); // you can find window. name = "my type" when monitoring window. name. this indicates the window object.

 

// Then empty window. name and call myType through caller

Window. name = undeifned; // monitoring found window. name = undeifined

Varcaller = {};

MyType. call (caller); // monitor caller. name = "my type", window. name = undefined

If you are not familiar with call or apply, you can view the relevant information, which is not described in detail here. The above example shows what this really represents in the function. In short, it is the caller. It is called directly on the browser client, which usually indicates the window. Calling through call and apply means the specified call object, and using it as a method in instantiation means the instantiated object.

It should be noted that in client programming, the obj. func specified in the event or setTimeInterval is not called obj, but a delegate is specified. I have encountered such a problem, so I 'd like to explain it here.

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.