JavaScript Create objects

Source: Internet
Author: User

<script>

JS creates two types of objects:

1: Object literal

var o = {name: ' Jack ', age:18};

This form is like building a JSON, this form of object created

The equivalent of an object that is new through the constructor of object

2: Constructors

function person (name, age) {

THIS.name = name;

This.age = age;

}

var p = new Person (' Lucy ', 18);

This form of object creation is a bit similar to many other languages, and it is an object created from the New keyword.

The new keyword is followed by a constructor, which is much like Java, but the Javanew object is passed through the class's

constructor is created, but JS has no class

Here's a little exercise:

/*

1. Create a student class with a name, age, and gender attributes

2, the creation of a class category, which has a name attribute;

3, the class has an Add method, you can add students

4, there is a Findstubysex method, you can obtain students through gender

5, there is a Findstubyage method, you can get students by age

*/

  

Student class

function Stu (sname, sage, ssex) {

This.sname = sname;

This.sage = Sage;

This.ssex = Ssex;

}

Class classes

function Cls (CNAME) {

This.cname = CNAME;

Defines an array to encapsulate the added students

This.stus = [];

}

Ways to add students

Cls.prototype.addStu = function (stu) {

Add a student to a class

This.stus.push (Stu);

}

Ways to get students through sex

Cls.prototype.findStuBySex = function (ssex) {

Determine if the class has students

if (this.stus! = NULL | | this.stus.length > 0) {

To encapsulate the query.

var stuarray = [];

Traverse class Students

for (var i = 0; i < this.stus.length; i + +) {

Judging students ' gender

if (This.stus[i].ssex = = Ssex) {

Add the found students to the array

Stuarray.push (This.stus[i]);

}

}

Returns the query data

return stuarray;

}

}

Ways to get students through age

Cls.prototype.findStuByAge = function (Sage) {

if (this.stus! = NULL | | this.stus.length > 0) {

var stuarray = [];

for (var i = 0; i < this.stus.length; i + +) {

if (this.stus[i].sage = = Sage) {

Stuarray.push (This.stus[i]);

}

}

return stuarray;

}

}

Get a Class object

var cls = new CLS ("Frontend");

Get Student objects

var stu1 = new Stu ("CQF", 19, "male");

var stu2 = new Stu ("SAP", 18, "female");

var stu3 = new Stu ("ch", 18, "female");

var stu4 = new Stu ("Yjl", 19, "male");

Add student

Cls.addstu (STU1);

Cls.addstu (STU2);

Cls.addstu (STU3);

Cls.addstu (STU4);

Used to encapsulate the returned student array

var Stuarray;

Access to students through gender

Stuarray = Cls.findstubysex ("male");

Console output Results

Console.log (Stuarray);

Stuarray = Cls.findstubysex ("female");

Console.log (Stuarray);

Get students by age

Stuarray = Cls.findstubyage (18);

Console.log (Stuarray);

Stuarray = Cls.findstubyage (19);

Console.log (Stuarray);

/*

JavaScript is created without classes, at least not for now. The class mentioned in the above case is just the class in the concept, through the dynamic nature of JS,

Dynamically add several methods (Addstu (), Findstubyage (), Findstubysex ()) to the class prototype object (prototype).

*/

</script>

JavaScript Create 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.