JavaScript system: Object detail_javascript skills

Source: Internet
Author: User
Objects are actually a set of data and functions. An object can be created by executing the new operator and the name of the object type to be created. You can create a custom Object by creating an instance of the Object type and adding its attributes and (or) methods. Previous

In javascript, the object is king; almost everything in Javascript is an object or an object that is used as an object. After understanding the object, you can understand Javascript. In javascript, the reference type is a data structure used to organize data and functions together. It is also called a class. Reference types are sometimes called object definitions because they describe the attributes and methods of a class of objects.

Most of the reference type values are Object-type instances, and the Object is also the most commonly used type in javascript. Although Object instances do not provide many functions, they are ideal for storing and transmitting data in applications.

Create object

There are two types of Object creation methods

[1] Object constructor

Var person = new Object (); // if you do not pass parameters to the constructor, you can leave the var person = new Object; person. name = 'ba'; person. age = 29; // create an empty Object without attributes var cody1 = new Object (); var cody2 = new Object (undefined); var cody3 = new Object (null); console. log (typeof cody1, typeof cody2, typeof cody3); // creates a string, number, array, function, boolean, and regexconsole. log (new Object ('foo'); console. log (new Object (1); console. log (new Object ([]); console. log (new Object (function () {})); console. log (new Object (true); console. log (new Object (// \ bbt [a-z] + \ B /));

[Note] the Object () constructor itself is an Object, and the constructor is an Object created based on the Function constructor.

[2] Using Object literal

Javascript provides a shortcut called literal to create most native object values. Using the literal only hides

Basic Process

var person = {name : 'bai',age : 29,5 : true};

[Note] Using commas to separate different attributes in the object literal, but adding a comma after the last attribute will cause an error in IE7

Define an object using the object literal. The attribute name is automatically converted to a string.

// Same as var person = {'name': 'ba', 'age': 29, '5': true };

If the curly braces are left blank, you can define objects that only contain the default attributes and methods.

// Equivalent to var person = new Object (); var person = {}; [tips] using the Object literal to encapsulate multiple optional parameters function displayInfo (args) {var output = ''; if (typeof args. name = 'string') {output + = 'name: '+ args. name + '\ n';} if (typeof args. age = 'number') {output + = 'Age: '+ args. age + '\ n';} console. log (output) ;}; displayInfo ({name: 'nicklas ', age: 29}); displayInfo ({name: 'match '});

The preceding parameter passing mode is most suitable for passing a large number of optional parameters to a function. In general, although naming parameters are easy to process, multiple optional parameters are not flexible enough. Therefore, the parameter must be used for values, and the object literal is used to encapsulate multiple optional parameters.

Set object

There are two methods to access object attributes. You can use dot notation or brackets notation to obtain, set, or update the attributes of an object.

The two advantages of the brackets method are that attributes can be accessed through variables, and attribute names can be invalid Javascript identifiers.

[Note] the variable can contain Chinese characters. Because the Chinese character is equivalent to the English character, it can be written as person. White or person ['white'].

Var myObject = {123: 'zero ', class: 'foo'}; console. log (myObject ['20140901'], myObject ['class']); // 'zero ''foo' console. log (myObject.123); // Error

Values in square brackets are implicitly converted to strings for output if they are not of the String type. if they are of the String type, the original values are output if there are quotation marks. Otherwise, they are recognized as variables, if the variable is not defined, an error is returned.

Person [0] = 1; // The number in [] will not report an error, but will be automatically converted to the string person [a] = 1; // The elements in [] that conform to the naming rules of variables will be treated as variables, variables are not defined, and the error "person [''] = 2 "; // The Null String in [] will not report an error, actually exists and can be called, but the person [undefined, null, true, or false] = 4 is not displayed in the set on the right of the console; // No error is reported, it is automatically converted to the string person ['white'] = 6; // No error is reported.

Delete object

The delete operator can be used to completely delete an attribute from an object. Delete is the only way to delete a property from an object. Setting the property to undefined or null can only change the value of the property, rather than deleting the property from the object. Delete can only delete data under an object. The other five basic types of values cannot be deleted.

[Note] delete does not delete attributes found on the prototype chain.

var foo = {bar: 'bar'};delete foo.bar;console.log('bar' in foo);//false var a = 123;delete a;console.log(a);//123

If you declare variable a in the global state, which is equivalent to a data a in the window object, you can use window. a or a to assign a value to a, and window. the values of a and a are always equal, but they cannot be deleted.

var a;a = 10;console.log(a,window.a);//10 10window.a = 20;console.log(a,window.a);//20 20delete a ;console.log(a,window.a);//20 20delete window.a;console.log(a,window.a);//20 20

If you use window. B to declare and assign values (B is equivalent to declaring it under the window object). It can be deleted and deleted using delete B and delete window. B has the same effect. After deletion, console. log (B) prompts that the variable does not exist, console. log (window. b) the message "undefined" is displayed.

Window. B = 10; console. log (B, window. b); // 10 10 delete B; console. log (B); // error console. log (window. b); // undefined window. B = 10; console. log (B, window. b); // 10 10 delete window. b; console. log (B); // error console. log (window. b); // undefined

Object nesting

Objects can be nested, but must be layer-by-layer

var student = {name : {chinese : 1,englisth : 2},sex : 1,age : 26}

[Note] the value can only be obtained at one layer, such as student. name. chinese. student. chinese can be used directly instead of passing through the name, because there may be chinese elements at the same level as the name.

var object1 = {object1_1:{object1_1_1:{foo: 'bar'},object1_1_2:{}},object1_2:{object1_2_1:{},object1_2_2:{}}};console.log(object1.object1_1.object1_1_1.foo);//bar

Instance method

Constructor: stores the function used to create the current object.
HasOwnProperty (propertyName): used to check whether a given property exists in the current object instance rather than in the instance's prototype. The propertyName must be specified as a string.

IsPrototypeOf (object): used to check whether the input object is a prototype of the input object.

PropertyIsEnumerable (propertyName): used to check whether a given attribute can be enumerated using the for-in statement. The propertyName must be specified as a string.

ToLocaleString (): indicates the string of the returned object, which corresponds to the region of the execution environment.

ToString (): String Representation of the returned object

ValueOf (): return the string, value, or Boolean value of the object, which is usually the same as the return value of the toString () method.

var myObject = {mark: true};console.log(myObject.constructor);//function Object(){}console.log(myObject.hasOwnProperty('mark'));//trueconsole.log(Object.prototype.isPrototypeOf(myObject));//trueconsole.log(myObject.propertyIsEnumerable('mark'));//trueconsole.log(myObject.toLocaleString());//[object Object]console.log(myObject.toString());//[object Object]console.log(typeof myObject.valueOf(),myObject.valueOf());// object Object{mark:true}

Summary:

Object Type

Objects are actually a set of data and functions. An object can be created by executing the new operator and the name of the object type to be created. You can create a custom Object by creating an instance of the Object type and adding its attributes and (or) methods.

var o = new Object(); 

Each instance of an Object has the following attributes and methods:

● Constructor -- stores the function used to create the current object.
● HasOwnProperty (propertyName) -- used to check whether a given property exists in the current object instance (rather than in the instance's prototype. The parameter property name must be specified as a string (for example, o. hasOwnProperty ("name "))
● IsPrototypeOf (object) -- used to check whether the input object is a prototype of another object
● PropertyIsEnumerable (propertyName) -- used to check whether a given attribute can be enumerated using the for-in statement.
● ToString () -- returns the string representation of the object
● ValueOf () -- returns the string, value, or Boolean value of the object. It is usually the same as the return value of the toString () method.

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.