JS elevation 6. Object-oriented Programming (2) Problems in creating objects _3 constructors

Source: Internet
Author: User

# The last time we talked about creating objects with the constructor's schema, we solved the problem of object recognition relative to Factory mode.

1 functionPerson (name,age,job) {2       This. name=name;3       This. age=Age ;4       This. job=job;5       This. sayname=function(){6Console.log ( This. Name);7      }8  }9 Ten  varperson1=NewPerson ("Xiaoming", "Doctor"); OnePerson1.sayname ();//xiaoming

# as above, it is the form of creating objects using the constructor pattern. The main problem with constructors is that each method is recreated on each instance . In fact, the quality can be equivalent to the following forms:

1 functionPerson (name,age,job) {2       This. name=name;3       This. age=Age ;4       This. job=job;5       This. sayname= new Function("Console.log (this.name)");6  }7 8  varperson1=NewPerson ("xiaoming1", "Doctor");9  varPerson2=NewPerson ("Xiaoming2", "Doctor");TenPerson1.sayname ();//xiaoming1 OnePerson2.sayname ();//xiaoming2

# from the above code, although Person1 and Person2 have a sayname method, but in fact two are different function instances. As follows:

1 console.log (person1.sayname==person2.sayname); // false

# This way, when we create many instance objects, it's the equivalent of creating a lot of different sayname () methods that do the same task at the same time, which is obviously not very good!

# Try moving the function definition outside the constructor, as follows:

1 functionPerson (name,age,job) {2       This. name=name;3       This. age=Age ;4       This. job=job;5       This. sayname=Sayname;6  }7 functionSayname () {8Console.log ( This. Name);9 }Ten  varperson1=NewPerson ("xiaoming1", "Doctor"); One  varPerson2=NewPerson ("Xiaoming2", "Doctor"); APerson1.sayname ();//xiaoming1 -Person2.sayname ();//xiaoming2 -  theConsole.log (Person1.sayname==person2.sayname);//true

# as above, it solves the problem that multiple functions do the same function, Person1 and Person2 actually share the Sayname function in the global scope.

# But there are the following issues:

(1) The Sayname function defined in the global scope is actually called by the person object, which makes the global scope a bit of a misnomer.

(2) If you need to define many methods in an object, it means that you need to define many global functions.

(3) The person this custom object type has no encapsulation to speak of.

~ ~ can solve the above problem through the prototype mode.

JS elevation 6. Object-oriented Programming (2) Problems in creating objects _3 constructors

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.