js--creating objects

Source: Internet
Author: User

A new instance of the object has been created and four properties have been added to it:

person=New Object (); // do not varperson.firstname= "Bill";p erson.lastname= "Gates";p erson.age=56; Person.eyecolor= "Blue";

Alternative code:

var person={firstname: "John", LastName: "Doe", Age:50,eyecolor: "Blue"};

Using the Object Builder:

function Person (Firstname,lastname,age,eyecolor) {this. firstname=FirstName; this. lastname=LastName; this. age= age; this. eyecolor=Eyecolor;} var tercher=New person ("Bill", "Gates", "page", "Blue");

Methods for defining objects inside the constructor function:

function Person (Firstname,lastname,age,eyecolor) {this. firstname=FirstName; this. lastname=LastName; this. age= age; this. eyecolor=Eyecolor; this. changename=changename; function ChangeName (name) {this. lastname=name;}}

Call:

Mymother.changename ("Ballmer");

Iterate through the properties of an object:

<! DOCTYPE html>function myFunction () {var  x; var txt= ""; var person={fname: "Bill", lname: "Gates", age:56 for the person ) {txt= TXT + person[x];} document.getElementById ("Demo"). innerhtml=txt;} </script></body>

Results: BILLGATES56

Reference: JS object creation, JS Create class and Object

js--Create object

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.