The concept of javascript--objects-creating objects

Source: Internet
Author: User
Tags ming

First, create the object

1. Create an empty object

Way One:

var o ={};o; Object {}

typeof (O); "Object"

Way two:

var o=new Object (); O;//object {}

typeof (O);//"object"

2. Create Non-empty objects

There are two ways to create objects in JavaScript: Object text notation, constructor function definition objects;

Method One: Object text notation:

The first example:

  

1     varhero= {2Name: ' Xiaoming ',3Sex: ' Male ',4Sayfunction(){return  This. name+ ': Hello! ‘;}5     };6Hero.name;//"Xiao Ming"7Hero. Sex;//male "8Hero.say ();//"Xiao Ming: Hello!" "9hero[' name '];//"Xiao Ming"Tenhero[' Sex '];//male " Onehero[' say '] ();//"Xiao Ming: Hello!" " A     varA= ' name '; Hero[a];//"Xiao Ming"
creating objects--text tagging method

Using text object tagging to define objects you should be aware of the following issues:

    • The {} is used when defining an object, but unlike a function, there is no keyword function;
    • Object Internal format: attribute name: attribute value, separated by "," between different attribute values;
    • Inside the object this represents the current object;
    • There are two ways to invoke the methods and properties of an object: Hero.name; and hero[' name ']; The latter type of array-like approach can be used in a dynamic manner

Method Two: Constructor function method:

Example:

1     functionpeople (name)2     {3            This. name=name;4            This. sex= ' man ';5            This. say=function(){return  This. name+ ': Hello! ‘;};6     }7     varhero=NewPeople (' Xiao Qiang '));8Hero.name;//"Xiao Qiang"9Hero.sex;//"Male"TenHero.say ();//"Xiao Qiang: Hello!" " Onehero[' name '];//"Xiao Qiang" Ahero[' sex '];//"Male" -hero[' say '] ();//"Xiao Qiang: Hello!" " -     vara= ' sex '; Hero[a];//"Male"
Create Object--constructor function method

Using text object tagging to define objects you should be aware of the following issues:

    • constructor functions are similar to functions: they all have function, but constructors have this to point to the current object, otherwise they cannot be called;

The difference between the text object method and the constructor function method:

Text Object method Constructor Function method
Direct creation of objects without instantiation Creates a constructor function that is used to create an object, and must instantiate the object with constructor function method
You can create only one You can instantiate multiple objects, and you can create objects with different contents by using the parameters of the constructor function

Ii. Abolition of objects

For the JavaScript language, it has a useless storage unit collection program, which means that we can free up memory without having to destroy the object specifically. If you do not have a reference to an object, you can destroy all the objects when you run the garbage collection program. But as a good habit, all references to objects are set to null, and objects can be forcibly revoked. For example: Hero=null; When the object variable hero is set to NULL, a reference to the first created object does not exist. This means that the next time the garbage collector is run, the object will be destroyed.

Note: Be careful when you revoke all references to objects. If an object has two or more references, to properly revoke the object, you must set all its references to null.

Early binding and late binding

The so-called binding, which is the method of combining an object's interface with an object instance.

Early binding (early binding) refers to defining its properties and methods before instantiating an object, so that the compiler or interpreter can convert machine code ahead of time. In languages such as Java and Visual Basic, with early binding, IntelliSense can be used in the development environment (that is, the ability to provide developers with a list of properties and methods in objects). ECMAScript is not a strongly typed language, so early binding is not supported.

Late binding (late binding) refers to the type of object that the compiler or interpreter does not know before it runs. With late binding, you do not need to check the object's type, just check whether the object supports properties and methods. All variables in the ECMAScript are in late binding methods. This allows a large number of object operations to be performed without any penalty.

Very late binding (Very late binding)

Technically, there is no very late binding at all. In this book, the term describes a phenomenon in ECMAScript, which is the ability to define a method after an object is instantiated. For example:

var o = new Object (), Object.prototype.sayHi = function () {  alert ("Hi"),};o. Sayhi ();

In most programming languages, you must define the methods of an object before instantiating it. Here, the method Sayhi () is added after creating an instance of the Object class. In the traditional language, not only have not heard of this operation, have not heard that the method will also be automatically assigned to an instance of object objects and can immediately use (the next line).

Note: It is not recommended to use the very late binding method because it is difficult to track and record it. However, you should be aware of this possibility.

The concept of javascript--objects-creating 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.