Daily js-Object

Source: Internet
Author: User

This article is not a javascript tutorial or preaching article. It is purely a personal record of some of the experiences of using js in your work.
 
As mentioned in the javascript authoritative guide, everything in javascript is an object. Let's summarize the objects in the next section, how to define them, how to use them, and how to expand them.
 
1. Define an object
First, we need to know how to define them.

1.1 currently the most popular json object: Used to facilitate interaction with ajax.
Var json = {"name": "hc", "sex": "1 "};
// Use www.2cto.com
Alert (json. name + "" + json. sex );
Why is json so popular? Because before I get into json, I use ajax to return data and parse text strings through "|". For example:
Var strResult = ajax. responseText;
// Ajax. responseTex is used to store the ajax returned text. 1 | id = 'ffffff' | name = 'ccc 'If I want to obtain the name, I need var attr = strResult. split ('|') [2]; this is neither intuitive nor object-oriented. So when I access json. Replace it with the string splitting method.
 
At the same time, the json string format requirements are loose. The name of var json = {"name"} above can be double quotation marks, single quotation marks, or even no quotation marks. However, if you use jQuery and other out-of-band frameworks for parsing, you must add double quotation marks. If you use the loose Method for parsing, var json = eval ("(" + str + ")"); // This requires a minimum value, but is easy to inject.
1.2 native object: object
 
Var obj = new Object ();
// The equivalent json declaration method is
Var obj = {};
// In various js frameworks, there is basically no way to write new Object (), which is written in obj = {}; simple and clear
 
1.3 objects of the most similar class: function
Function User (name, sex ){
// Declare the attributes of the class.
 
This. name = name;
 
This. sex = sex;

// This is a public method of declarative classes.
This. getMyFullName = function (){
Return _ firstname + this. name;
}

// This is the private variable that defines the class
Var _ name = name;
Var _ firstname = "yellow ";

// This is the private method for defining the class
Function getSex (){
}
}
Define a class. The next step is the instance. Like C #, It is instantiated through the new class.
Var my = new User ("hc", "1 ");
Alert (my. name );
Alert (my. getMyFullName ());

2. Extension object
In each js framework, objects are basically defined in these several ways. After we know how to define the object. You need to know how to expand them.
 
There are two types of objects to be extended: one is to extend the entire class, and the other is to extend the instantiation of only a class.

 
2.1 prototype attributes

The prototype attribute is generated when javascript is used to meet our need to expand the entire class.
Prototype is the prototype that the property points to the current object.
Definitions and concepts are not easy to understand. For example. If you want to add a method to all strings or arrays, who are you looking?
Microsoft will not add a method to you. So prototype comes in handy at this time.
String. prototype. getTempMethod = function (){
Return "hc ";
}
Var str = "c ";
Alert (str. getTempMethod (); // you have your own exclusive method in string.

2.2 Private Extension
 
If you only want to extend a method to your current object, it is very simple, because all the methods and fields in js are attributes. Even if there is no property method, you can directly use it.
But its value is undefined.

// At this time, the alert method is undefined.
Var obj = {};
// At this time, an alert method is defined on this object.
Obj. alert = function (){{
}

From ksh. xy
 

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.