Creating JavaScript objects

Source: Internet
Author: User

Create a new instance person and add four attributes to it:

1 person=new  Object (); 2 person.firstname= "Bill"; 3 person.lastname= "Gates"; 4 person.age=56; 5 person.eyecolor= "Blue";

Person{firstname: "John", LastName: "Doe", Age:50,eyecolor: "Blue"};

1 function Person (Firstname,lastname,age,eyecolor) {2this. first=FirstName; 3 this. lastname=LastName; 4 this. age= age; 5 this. eyecolor=eyecol

After you create a JavaScript object, you can create an instance of the object:

1 var New person ("Bill", "Gates", "a", "Blue"); 2 var mymother=New person ("Steve", "Jobs", "green");

Add a method to a JavaScript object

1 functionPerson (Firstname,lastname,age,eyecolor) {2  This. firstname=FirstName;3  This. lastname=LastName;4  This. age=Age ;5  This. eyecolor=Eyecolor;6 7  This. changename=ChangeName;8 functionChangeName (name) {9  This. lastname=name;Ten } One}

JavaScript class

JavaScript classes are object-oriented languages, but JavaScript does not use classes

In JavaScript, classes are not created and objects are not created through classes

JavaScript is based on prototype (prototypes) instead of class-based

JavaScript for: In loop

For...in iterates through each property in the object

1 var person=={fname: "Bill", lname: "Gates", Age "" "; 2 3 For (x in person) 4 {5txt=txt+person[x]; 6 }7 document.getElementById ("Demo"). Innerhtml=txt;

The output will be:

Billgates56

Creating JavaScript objects

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.