On JavaScript objects

Source: Internet
Author: User
Tags hosting

JavaScript ObjectsObject

ECMA-262 defines an object as an unordered collection of properties, each of which holds an original value, object, or function. Strictly speaking, this means that the object is an array of values that are not in a particular order.

objects are unordered collections of properties and methods

Object is a collection of unordered properties

Although ECMAScript defines an object so much, its more general definition is the representation of a noun (person, place, or thing) based on the code.

In JavaScript, in addition to the five primitive types of numbers, strings, booleans, nulls, undefined, all are objects, which confirms the saying "All things are objects".

An object is a composite value that aggregates many values together, which may be primitive types or other objects, and can be accessed through property names, which are arbitrary strings that can contain an empty string.

A JavaScript object is also a data structure that you can use to test the properties of an object in its prototype chain for the existence of a constructor in the instanceof operator prototype .

type of object: in ECMAScript, all objects are not created equally.

In general, there are three types of objects that you can create and use:

1. Local objects (native object)

ECMA-262 defines local objects (native object) as "objects provided by ECMAScript implementations that are independent of the hosting environment." In simple terms, a local object is a class defined by ECMA-262 (reference type).

Object, Function, Array, String, Boolean, number, Date, REGEXP, Error, Evalerror, Rangeerror, Referenceerror, SyntaxError, TypeError, Urierror

2. Built-in objects (built-in object)

ECMA-262 defines the built-in object (built-in object) as "all objects that are provided by the ECMAScript implementation, independent of the hosting environment, and appear when the ECMAScript program starts executing." This means that the developer does not have to instantiate the built-in object explicitly, it has been instantiated. ECMA-262 only defines two built-in objects, Global and Math (they are also local objects, by definition, each built-in object is a local object).

3. Host object (Host objects)

Which is defined by a host environment (such as a browser) embedded in the JavaScript interpreter, such as HtmlElement;

Object properties:

1. Own attribute (ownproperty): The attribute defined directly in the object;

2. inherited attributes (inheritedProperty): Properties defined in the object's prototype object

requirements for object-oriented languages

An object-oriented language requires the developer to provide four basic capabilities:

    1. Encapsulation-the ability to store relevant information (regardless of data or method) in an object
    2. Aggregation-the ability to store an object within another object
    3. Inheritance-the ability to get properties and methods of a class by another class (or classes)
    4. Polymorphism-Ability to write functions or methods that can run in multiple ways

ECMAScript supports these requirements and can therefore be viewed as object-oriented.

JavaScript is also an object-based weakly typed language

Object-based: Built-in large number of objects, as long as a small amount of code can complete work

Weak type: Not strict on type requirements

the composition of the object

In ECMAScript, an object is composed of an attribute (attribute), which can be either the original value or a reference value. If the attribute holds a function, it is treated as a method of the object, otherwise the attribute is considered the property of the object.

Creation of objects

1. Object Direct Volume (literal)

The direct amount of the object is a mapping table of several name/value pairs, the middle of the name/value pair is separated by a colon, and the name/value pairs are separated by commas, and the entire mapping table is enclosed in curly braces. The property name can be either a JavaScript identifier or a string literal, which means that the following two creation objects obj are written exactly the same way:

var obj = {x:1, y:2}; var obj = {' x ': 1, ' Y ': 2};

2. creating an Object from new (constructor mode)

The new operator follows a function call, which is a constructor that creates and initializes an object.

var computer=New  Object (); computer.size=15.6; Computer.color= ' black '; Computer.price= ' 6999¥ '; Computer.brand= ' DELL '; computer.open=function  () {    Console.log (' smash ');} Computer.close=function  () {    console.log (' the need to buy a new machine because the shutdown was broken);}

3.object.create ()

ECMAScript5 defines a method named, which Object.create() creates a new object, where the first parameter is the prototype object of the object

the constructor of the object

Custom constructors

constructors, which are essentially functions, are declared with the function keyword

Naming: Naming with the name of the object you want to describe, capitalized (by convention) Use this instead of the object in the constructor

functionStudent (number,age,gender,name,level) { This. number=Number ;  This. age=Age ;  This. gender=gender;  This. name=name;  This. level=Level ;  This. dohomework=function() {Console.log (' Hello everyone, I am ' + This. level+ ' + This. name+ ', a car-loving ' +gender); }     This. runoutclass=function() {Console.log (' Skipping class because I like driving '); }}varstu1=NewStudent (' 20 ', 18, ' man ', ' brother K ', ' third grade ')); Stu1.dohomework (); Stu1.runoutclass (); Console.log (STU1) ;

This keyword

1. Inside the constructor: This is the current object within the constructor function

2, inside the normal function: When the function is called, this points to the object calling this function, who calls this function (method), the function of this is who

New Keyword

What has been done:

1. An object is created and the object is present in this

 This New Object ();

2. Execute the code in the constructor to add properties and methods to the Created object

3. Return this to the place where the constructor was called

return this;

Accessing the properties of an object

1, through the point of the way: object. Properties

2, by key way: Object [property corresponding name string]

Traversing the properties of an object using the for-in format

Format:

For (var property name in the object to traverse) {

The code you want to write

}

basic types and reference types

Basic type:

Number, String, Boolean, Undefined, null

Reference type: Object

The difference between basic and reference types

1, the location of the storage is not the same

The base type is stored on the stack.

The reference type is stored on the heap

2. When the communication takes place

The basic type is a copy of the Enter function to participate in the operation

A reference type that replicates the memory address stored on the stack, which points to the object in the heap. So when the function executes, it's actually manipulating the same object.

Basic type parameter, does not change the argument's

Reference type parameter changes the actual argument

On JavaScript objects

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.