In-depth understanding of JavaScript Object series The first--a preliminary knowledge object

Source: Internet
Author: User

xTable of contents [1] definition [2] creation [3] composition [4] references preceding words

The difficulty in JavaScript is functions, objects, and inheritance, as described in the previous series of functions. Starting from this series, this article is the first article in this series--the primary object

Object definition

The basic data types for javascript include undefined, null, Boolean, string, number, and object. objects are different from other primitive type values, an object is a compound value: it aggregates many values (primitive values or other objects) and accesses them by name

Objects can also be considered an unordered collection of attributes, each of which is a name/value pair. The property name is a string, so we can think of the object as a mapping from string to value

For a detailed distinction between composite and original values.

Object creation

There are three ways to create objects, including the new constructor, the object direct amount, and the object.create () function

"1" New constructor

Use the new operator followed by the object constructor to initialize a newly created object

 var  person = new   //  Person.name = ' Bai ' ;p erson.age  =; 
// create an empty object without attributes var New Object (); var New Object (undefined); var New Object (null); Console.log (typeof Cody1,typeoftypeof Cody3) ; //  Object Object Object

If the argument is an object, the object is returned directly

var o1 = {a:1}; var New  = = = O2); // true var function (){}; var New  = = = F2); // true

If it is a value of the original type, the wrapper object corresponding to that value is returned

// String {0: "F", 1: "O", 2: "O", Length:3, [[Primitivevalue]]: "foo"}console.log (new Object (' foo ')) ; // Number {[[Primitivevalue]]: 1}Console.log (new Object (1)); // Boolean {[[Primitivevalue]]: true}Console.log (new Object (true));

"2" Object literal

JavaScript provides a shortcut called a literal to create most native object values. Using literals simply hides the same basic process as using the new operator, so it can also be called a syntactic sugar

The object literal is a mapping table consisting of several name/value pairs, separated by colons in the middle of the name/value pair, enclosed in curly braces for the entire mapping table.

Different attributes are separated by commas, the property name can be any string, the property value can be any type expression, the value of the expression is the property value

// equivalent to var person = new Object (); var
var person = {    ' bai ',    +,    true};

Use object literals to define an object, and the property name is automatically converted to a string

// Ibid . var person = {    ' name ': ' Bai ',    ' age ': +,    true

[note] Generally, the comma after the last attribute of the object literal is ignored, but causes an error in the Ie7-browser

// ie7-error SCRIPT1028 in browser: Missing identifier, string, or number var person = {    ' bai ',    +,    true,};

"3" object.create ()

ES5 defines a method named Object.create (), which creates a new object, the first parameter is the prototype of the object, and the second optional parameter is used to further describe the properties of the object.

var // O1 inherits the attributes X and yconsole.log (o1.x); // 1

You can create a new object without a prototype by passing in the parameter null, but objects created in this way do not inherit anything, not even the underlying method. such as ToString () and valueof ()

var o2 = Object.create (null//  O2 does not inherit any properties and methods var o1 = {};console.log ( Number (O1)); // NaNconsole.log (number (O2)); // uncaught typeerror:cannot Convert object to primitive value

If you want to create an ordinary empty object (such as an object created by {} or New object), you need to pass in the Object.prototype

var // O3 and {} are the same as new Object () var o1 = {};console.log (number (O1)); // NaNConsole.log (number (O3)); // NaN

Object composition

An object is an unordered collection of properties, consisting of key names and property values

"Key Name"

All the key names of the object are strings, so no quotation marks are allowed, and if not the string is automatically converted to a string

var o = {  ' P ': ' Hello World '}; var o = {  ' Hello World '};
var o ={  1: ' A ',  3.2: ' B ',  true,  True ,  . true ,   true ,}; // Object {1: "A", 100:true, 255:true, 3.2: "B", 0.01:true, 0.234:true}o;

[note] If the key name does not match the identifier naming rules, you must enclose the quotation marks, or you will get an error

// uncaught syntaxerror:unexpected identifier var o = {    123}var o = {    ' 1p ': 123}

"Property Value"

The property value can be any type of expression, and the result of the final expression is the result of the property value

var o ={    1+2}console.log (O.A); // 3

If the property value is a function, this property is often referred to as the "method"

var o = {  function  (x) {    return 2 * x;  }};o. P (1); // 2

Because the object's method is a function, there is also a Name property. The Name property of the method returns the name of the function immediately following a function keyword. If it is an anonymous function, the ES5 environment returns the UNDEFINED,ES6 environment returns the method name

var obj = {  function  f () {},  function//  " F "//es5:undefined//ES6:" M2 "

referencing objects

If different variable names point to the same object, then they are all references to the object, that is, to the same memory address. Modifying one of the variables affects all other variables

var o1 = {}; var o2 == 1; Console.log (o2.a); // 1o2.b = 2; console.log (o1.b); // 2

If you cancel a reference to a variable for the original object, it does not affect the other variable

var o1 = {}; var o2 == 1; Console.log (O2); // {}

Resources

"1" W3school-javascript advanced Tutorial--Reference type http://www.w3school.com.cn/js/pro_js_referencetypes.asp
"2" Ruan one peak JavaScript standard reference Tutorial--Object http://javascript.ruanyifeng.com/grammar/object.html
"3" JavaScript authoritative Guide (6th edition), Chapter 6th object
"4" JavaScript Advanced Programming (3rd Edition), chapter 5th reference type
5 "javascript Statement Essence" Chapter 3rd Object
6 "JavaScript Object-Oriented Essentials", chapter 3rd Understanding objects
"7", "You don't know the volume of JavaScript," chapter 3rd Object

In-depth understanding of JavaScript Object series The first--a preliminary knowledge object

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.