JS Object-oriented

Source: Internet
Author: User

Method one for declaring an object (directly assigned by a constant value)

        varXiaoming = {//declares an object (declared directly by a constant xiaoming in the form of a {} assignment)Name: ' Xiaoming ',//a property property of an object is separated from the property by a comma. AGE:40,//an attribute of the objectSex: ' Male ',//an attribute of the objectJobfunction(){//One method of the objectAlert (' Xiaoming is good at acting '); return' Object method return value ';        }} alert (Xiaoming.name); //xiaoming.job ();//method of invoking the Xiaoming object result: Xiao Ming is very good at acting.Alert (Xiaoming.job ());//Object method Return value Result: Object method return value        //called if the method property of the object is appended ()

Method two for declaring an object (via new object () declaration)

        varXiaoming =NewObject ();//another way to declare an objectXiaoming.name = ' Xiao Ming ';//this method uses the. Property = property value. To represent the properties of an objectXiaoming.age = 40;//declaring a property of a Xiaoming objectXiaoming.sex = ' Male ';//declaring a property of a Xiaoming objectXiaoming.job =function(){//one way to declare a Xiaoming objectAlert (' Xiaoming is good at acting '); } alert (xiaoming.name);//Call Xiaoming A property of this object. Result: Xiao MingXiaoming.job ()//the method that calls xiaoming this object results: Xiao Ming is very good at acting.

Method three for declaring an object (with the object () declaration omitting new)

        varXiaoming = Object ();//the third way to declare an object xiaoming. Xiaoming.name = ' Xiao Ming ';//declaring a property of a Xiaoming objectXiaoming.age = 40;//declaring a property of a Xiaoming objectXiaoming.sex = ' Male ';//declaring a property of a Xiaoming objectXiaoming.job =function(){//a method (function) for declaring a Xiaoming objectAlert (' Xiaoming is good at acting '); } alert (Xiaoming.age); //40 calling a property of the Xiaoming object results:Xiaoming.job ();//Call Xiaoming a method (function) of this object result: Xiao Ming is very good at acting.

//Supplement the. property is exactly equivalent to [' property ']

        var xiaoming = Object ();        xiaoming[' name '] = ' Xiao Ming ';          xiaoming[//xiaoming["age", like the Xiaoming.age effect, is a property of an object, but it's just two ways to do it.         xiaoming[' sex ' = ' male '        xiaoming[function() {            alert (' xiaoming is very good at acting ') );        }        Alert (xiaoming[' age '); //  +        // Xiaoming is a good actor .

Summary: Objects (both to store data and to store functions)

JS Object-oriented

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.