07.30 JavaScript--js Three ways to create objects

Source: Internet
Author: User

1. The first way to create an object: first define the class and then create the object

The first way to create an object: first define the class and then create the object
function Stu (Sno, name, age) {
This.sno = Sno;
THIS.name = name;
This.age = age;
This.study = function () {
Alert ("+this.sno+" student, named "+this.name+", is studying hard, age is "+this.age");
}
}

var a = new Stu (1, ' Wang ', 23);
A.study ();

2. Second way to create objects: Use object timing to create objects

// second way to create objects: Use object timing to create objects            var New Object ();             = 1;             = "Wang";             = all;             function () {                alert ("sno+" + this.), the name is "+ this. name+", is studying hard, the age of "+ this  . Age);            }                        Obj.study ();             Delete Obj.name;            Obj.study ();

3. Third Way to create objects: creating objects using JSON syntax

Json:javascript Object Notation

Third Way to create objects: using JSON syntax to create objects
var stu = { "sno": 1, "name": "Wang" , "age": function() { alert ("sno+" + this.), the name is "+ this. name+", is studying hard, age is " +this. age);} } Stu.study ();

07.30 JavaScript--js Three ways to create 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.