Object of JavaScript Reference type

Source: Internet
Author: User
Tags naming convention

reference type : In JavaScript, a reference type is a data structure that organizes data and functionality together and is often referred to as a class. Reference types are also sometimes referred to as object definitions because they describe the properties and methods that a class of objects have.
  Reference object's Value (object): is an instance of a reference type. An object is an instance of a particular reference type that is created with a constructor after using the new operator. The constructor itself is a function, except that the function is defined for the purpose of creating a new object. An alias for an object is a collection.

Object: is a set of data and functions

[1] Object creation: How to create two types of object
[1.1] using the new operator followed by the object constructor

var person = new Object (); similar to var person = new object;//means that you can do without parentheses person.name = ' Bai ';p erson.age = 29;

[1.2] Using object literals

var person = {    name: ' Bai ',    age:29}

[Note 1] Use commas in the object literal to separate different attributes, but adding commas after the last attribute will cause errors in IE7 and earlier versions and opera

[Note 2] The object literal is used to define the object, the property name is automatically converted to a string, and the object constructor is not actually called
[Note 3] If you leave the curly braces blank, you can define an object that contains only the default properties and methods
e.g. var person = {}; equals var person = new Object ();
[1.2.1] uses the object literal to encapsulate several optional parameters:

function DisplayInfo (args) {    var output = ';    if (typeof args.name = = ' string ') {        output + = ' Name: ' + args.name + ' \ n ';    }    if (tyoepf args.age = = ' number ') {        output + = ' Age: ' + args.age + ' \ n ';    }    alert (output);} DisplayInfo ({    name: ' Nicholas ',    age:29});

[note] These modes of passing parameters are best suited for situations where a large number of optional parameters are required to be passed to the function. As a best practice, use formal parameters for those required values, and use object literals to encapsulate multiple optional parameters.


[2] Properties and methods for instance of object
[2.1]constructor: Saves the function used to create the current object
[2.2]hasownproperty (PropertyName): Used to check whether a given property exists in the current object instance, rather than in the prototype of the instance. Where PropertyName must be specified as a string
[2.3]isprototypeof (object): Used to check if an incoming object is a prototype of an incoming object
[2.4]propertyisenumerable (PropertyName): Used to check whether a given property can be enumerated using a for-in statement. Where PropertyName must be specified as a string
[2.5]tolocalestring (): Returns the string representation of an object that corresponds to the region of the execution environment.
[2.6]tostring (): Returns the string representation of an object
[2.7]valueof (): Returns the string, numeric, or Boolean representation of an object, usually the same as the return value of the ToString () method

[3] Two methods of accessing object properties
[Note 1] It is recommended that you use dot notation unless you have to use variables to access the property, and that the name of the property name should conform to the naming convention
[Note 2] There can be Chinese in the variable because Chinese is equivalent to a character and is treated the same as English characters, so it can be written as a person. White or person[' white '
[Note 3] the value in square brackets if the non-string type is implicitly converted to string and then output using string (), if it is a string type, if the original value output, otherwise it will be recognized as a variable, if the variable is not defined, the error

Person[0] = 1; The number in [] does not error, but is automatically converted to string person[a] = 1; [] The element that conforms to the variable naming rule is treated as a variable, the variable is not defined, and the error person['] = 2; The empty string in [] does not error, is actually present and can be called, but does not display person[undefined or null or TRUE or FALSE in the collection on the right side of the console = 4;//does not error, but is automatically converted to a string person[' white ' = 6; No error.

[3.1] Person.name
[3.2]person[' name ']
[3.2.1] The main advantages of square brackets
[A] The property can be accessed through a variable
[B] Property name can contain a character that causes a syntax error, or a keyword or reserved word is used in the property name
e.g. person[' first name '];

[4] objects can be nested, but must be value-by-layer

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

[note] The value can only be taken one layer at a level, such as Student.name.chinese, but not across name, directly with Student.chinese, because with the name of the sibling may also have an element called Chinese

[5] Delete operator of the object: Delete can only delete the data under the object, the value of the other 5 underlying types is not dropped
[5.1] If the variable A is declared in the global state, the equivalent of a Window object under a data A, you can use WINDOW.A or a to assign a value, and the values of WINDOW.A and a are always equal, but it is not possible to delete
[5.2] If the window.b to declare and assign a value (b equivalent to declare under the Window object), can be deleted, and with the same effect as delete B and delete window.b, after deletion, Console.log (b) Prompt variable does not exist, Console.log (window.b) Tip undefined

Object of JavaScript Reference type

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.