The three most common methods of JS object definition

Source: Internet
Author: User

Defining objects: The combination of properties and methods (combinations of variables and functions)
1. (* * *) var obj = {}

2.var obj = new Object ();

3. Defining objects with function

Specific examples are:

1 //1. Defining an Obj Object2 //obj: Object name3 var obj = {4 //Height,weight,sex properties called Objects5 height:170,//use, separating properties and methods6 weight:90,7 sex: ' BBW ',8 9 //Say (): Method called ObjectTen say:function () { One alert (' 付帅帅, you have a long handsome '); A         }, - eat:function () { - alert (' eat '); the         } -     } -  - //Gets the object's properties (object name. Property name) + Console.log (obj.weight); - //method of the Calling object (object name. Method Name ()) +Obj.say ();
1 //2. Defines an empty object2 var O1 = new Object ();3 //Assigning properties to an object4 o1.height = ' 150cm ';5 o1.color = ' Silver ';6 O1.fire = function () {7 alert (' Chop it, when firewood Burns ');8     }9 Ten //Call One Console.log (o1.height); AO1.fire ();
3. Use the function to define    the Object function demo () {        //This represents the current object        this.memory = ' 16G ';        This.hard = ' 1T ';        This.ys = function () {            alert (' The computer is doing a high-load operation at this moment ');}    }    Call (Instantiate demo () function)    var d = new demo ();    Console.log (d.memory);    D.ys ();

The three most common methods of JS object definition

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.