Another talk about JavaScript user-defined objects __java

Source: Internet
Author: User
Tags object object javascript keycode
We have written a [log of the original]javascript object processing], which introduces some concepts of creating user-defined objects in JavaScript. However, I think the text of the prototype object, the object of the direct volume, simulation of namespaces and other concepts are inaccurate or not detailed, it is necessary to do some additional.

One, Object objects
In JavaScript, an object is an important local object that is the root object of other local objects (such as array, Date, string, and so on) and provides the basis for creating custom objects.

Var object=new object ();
After you create a custom object by using object, you can add properties and methods to the object. This can be seen in the previous log introduction.
After you create an object, you can access the properties and methods of the object:

Object. {Property|method (ArgList)}
1. Properties of Object objects
The prototype property returns a reference to the object type prototype.
The constructor property represents the function that created the object.

2, Object Objects method
hasOwnProperty method: Determines whether an object has a specific attribute.
isPrototypeOf method: Determines whether an object is a prototype of another object.
propertyIsEnumerable method: Determines whether a given property can be enumerated with a for-in statement.
ToString method: The return value for the object object is ' [Object] '.
ValueOf method: Same as the ToString method return value.
The properties and methods of object objects will be overwritten by other classes.

Second, improve the constructor function
As mentioned earlier, you can create a new object instance by creating a constructor and then by using the new operator and the constructor.
Regardless of whether you are using the object's constructor directly or defining your own object constructor, there is the same problem, that is, when you create a new object instance, the function is defined repeatedly. (Do you understand that?) I think it's easier to understand than the previous formulation. )
To solve this problem, you can refine the constructor by using the object's prototype to define the constructor in the following ways:

function Constructorname (argList) {
This.propertyname=value; Set the properties of an object
if (typeof constructorname._initialized== "undefined") {
Constructorname.prototype.methodname=function (argList) {
Body
}
Constructorname._initialized=true;
}
}
In an improved constructor, you can ensure that you do not repeat the definition of a function when you add a method to a class by examining and setting the value of the _initialized property. Thus, when creating a new object instance, the method of the prototype object can be used by the instance, and the resource is saved without the need to duplicate the definition function.

Direct quantity of Object
When using the object's direct quantity, you should enclose the attribute and property values by curly braces, the property name and property values are separated by colons, and the property-value pairs are delimited by commas.
For example:

var object={
"PropertyName": "Value",
' MethodName ': function (argList) {statements}
};
In JavaScript, an object reference can also be used as a parameter of a function, usually with the following two ways:
Reference 1, to pass an object to a function, you can create an object instance based on the constructor and store the object's reference in a variable, and then use the variable as the actual parameter in the function call;
2. Use the object's direct amount as the actual parameter in the function call.
In contrast, the latter approach is obviously easier because neither the constructor nor the explicit creation of an object instance is required.

Third, simulate a namespace
In object-oriented languages, namespaces are often used to organize classes to avoid name collisions. In JavaScript, you can simulate namespaces through objects. When you create a JavaScript library, you can wrap object definitions within namespaces without having to define global functions and classes.
When you use an object to simulate a namespace, you first create an empty object as a namespace, and then define the constructor in this namespace.
Here is the code that simulates the namespace:

var myns={};
Myns.constructorname=function (argList) {//define a constructor in the namespace
statments; Write code here to add properties and methods to the object
};
var object=new myns.constructorname ("argList");
Of course, as needed, you can also create nested namespaces and then pass "." can be called. Related Log
Original Problems with IE and FF to get class attributes
Original The Window.settimeout () method of JavaScript
Original JavaScript in operator
Original JavaScript keycode and keyboard corresponding tables
[Original] Understand the node properties of the DOM again
Related 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.