JS Note 2--defining objects

Source: Internet
Author: User

There are several ways to define objects in 16.javascript (there is no concept of classes in JavaScript, only objects):

1) extend its properties and methods based on existing objects:

var New Object ();
= "Zhangsan";
function (name) {
  this. Name = name;
Alert (this. Name);
}
Object.sayname ("Lisi");

2) Factory mode:

//factory-style object creationfunctionCreateObject () {varObject =NewObject (); Object.username= "Zhangsan"; Object.password= "123"; Object.get=function() {alert ( This. Username + "," + This. Password); }returnobject;}varObject1 =CreateObject ();varObject2 =CreateObject (); Object1.get ();//method of constructing with parametersfunctionCreateObject (Username,password) {varObject =NewObject (); Object.username=username; Object.password=password; Object.get=function() {alert ( This. Username + "," + This. Password); }returnobject;}varObject1 = CreateObject ("Zhangsan", "123"); Object1.get ();//let a function be shared by multiple objects, rather than having a function object for each objectfunctionget () {alert ( This. Username + "," + This. password);}functionCreateObject (Username,password) {varObject =NewObject (); Object.username=username; Object.password=password; Object.get=get; returnobject;}varObject1 = CreateObject ("Zhangsan", "123");varObject2 = CreateObject ("Lisi", "456"); Object1.get (); Object2.get ();

3) Constructor Method:

functionPerson () {//before executing the first line of code, the JS engine generates an object for USObject.username= "Zhangsan"; Object.password= "123"; Object.getinfo=function() {alert ( This. Username + "," + This. Password); }//here is a hidden return statement that returns the previously generated object}varperson =NewPerson ();p erson.getinfo ();//parameters can be passed when constructing an objectfunctionPerson (Username,password) {//before executing the first line of code, the JS engine generates an object for USObject.username=username; Object.password=password; Object.getinfo=function() {alert ( This. Username + "," + This. Password); }//here is a hidden return statement that returns the previously generated object}varperson =NewPerson ("Zhangsan", "123");p erson.getinfo ();

4) Prototype ("prototype") mode:

//create an object using the prototype (prototype) methodfunctionPerson () {}person.prototype.username= "Zhangsan"; Person.prototype.password= "123"; Person.prototype.getInfo=function() {alert ( This. Username + "," + This. password);}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p erson1.username= "Lisi";p erson1.getinfo ();p erson2.getinfo ();//functionPerson () {}person.prototype.username=NewArray (); Person.prototype.password= "123"; Person.prototype.getInfo=function() {alert ( This. Username + "," + This. password);}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p Erson1.username.push ("Zhangsan");p Erson1.username.push ("Lisi");p Erson1.password= "456";p erson1.getinfo ();p erson2.getinfo ();//defining objects using the Prototype + constructor methodfunctionPerson () { This. Username =NewArray ();  This. password= "123";} Person.prototype.getInfo=function() {alert ( This. Username + "," + This. password);}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p Erson1.username.push ("Zhangsan");p Erson2.username.push ("Lisi");p erson1.getinfo ();p erson2.getinfo ();

5) Dynamic prototyping: In the constructor, all objects share a method through the flag amount, and each object has its own properties.

functionPerson () { This. Username =NewArray ();  This. password= "123"; if(typeofPerson.flag = = "undefined") {alert ("Invoked"); Person.prototype.getInfo=function() {alert ( This. Username + "," + This. Password); } Person.flag=true; }}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p erson1.getinfo ();p erson2.getinfo ();

JS Note 2--defining 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.