JS Object-oriented: creating objects

Source: Internet
Author: User
Tags hasownproperty

Recently in the study of "JS Advanced Program Design", before the many JS class library and jquery plug-ins will be used in the object-oriented way to design, but they still stay in the approach-oriented phase, so take a good record of the learning JS created object today.

First way: Factory mode

function Createperson (name,age,job) {    varnew  Object ();     = name;     = Age ;     = job;     function () {        alert (this. Name);       };     return o;   } // called var person = Createperson (' Zhang San ', ' $ ', ' IT ');p erson.sayname ();

Second approach: constructor mode

 function   person (name,age,job) { Span style= "color: #0000ff;"    >this . Name = name;       job;  this . Sayname = function   () {alert ( this  .name); };} var  person = new  person (' John Doe ', +, ' nothing ‘); 
var person2 = new person (' Li 42 ', 22, ' Nothing ');
Alert (Person.sayname = = Person2.sayname); // false

An instance of an object created in this way, each of which corresponds to a different name, age, Job attribute, and Sayname method.

For example, the Sayname method that outputs two instances above is a different object.

The Third Way: Prototype mode

functionPerson () {}person.prototype.name= ' Harry '; Person.prototype.age= 25; Person.prototype.job= ' Nothing '; Person.prototype.sayName=function() {alert ( This. name);};

Can also be improved to:

function Person () {}
Person.prototype = {
Name: ' Harry ',
Age:25,
Job: ' Nothing ',
Sayname: function() {alert (this. name);}
}varperson =NewPerson ();

The properties created by the prototype are shared between each instance, somewhat like the feeling of static.

The prototype of the prototype will have a property created by default: constructor, a pointer to the function that creates the prototype, and the previous example will be the person function.

But the improved wording will be different, Person.prototype.constructor does not point to the person function, the following method is equivalent to rewrite the prototype as an object object.

Alert (person instanceof person);//true

Alert (person instanceof Object);//true

Alert (Person.constructor = = person);//false

Alert (Person.constructor = = Object);//true

If the constructor property is important, you can specify it manually.

function= {    Constructor:person,    ' Harry ',     +,    ' nothing ' ,      function () {alert (this. name);}} var New Person ();

 var  person1 = new   person ();  var  person2 = new   person (); When the Person1 instance is redefined by name, the name of the Person1 instance changes, and the name value in the prototype remains the same person1.name  = ' King eight eggs ' ;alert (Person1.name);  //  King eight eggs  alert (Person2.name);  //  Harry  //  but use Delete to remove the instance properties and revisit the properties of the prototype  delete   Person1.name;alert (person1.name);  //  Harry 

//hasownproperty can detect if an instance has this property, inherited from Object
Alert (Person1.hasownproperty ("name")); //false
In can detect whether this property, whether it is an instance property or a prototype property
Alert ("name" in Person1); //true

JS Object-oriented: creating 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.