JavaScript Learning Summary-creating objects (6_ other methods)

Source: Internet
Author: User

We introduced a total of four methods to encapsulate the object (factory, construction mode, prototype mode, construction + prototype), and focus on the constructor + prototype model to make some improvements to make it more perfect, then in addition to these methods, there are two ways to encapsulate the object, or that sentence, Choose the right one (in fact, each method has its own pros and cons, when I first started to learn the total feeling is pay Paul).

First look at the first method

function Person (name,age) {  var o=New  Object ();  O.name=name;  O.age= Age;  O.sayname=function() {    Console.log (this. Name);  };   return o;} var person_01=New person ("Sakura", +); var person_02=New person ("Misaka", 20);

See this I guess you're probably thinking big brother you're blindfolded me? It's not a factory model. Yuck, this isn't a constructor.

This is the Factory mode + constructor blending mode, which is called the parasitic constructor pattern (MDZZ)

First we see this pattern on the basis of the original constructor added Factory mode, that is, inside the function created a new object o, and finally returned to the new object o

The book says that this pattern can be used to create constructors for objects in special cases, with an example

function Specarr () {  var values=New  Array ();  Values.push.apply (values,arguments);  values.topopedstring=function() {    returnthis. Join ("|" );  } return values;} var colors=New Specarr (' Red ', ' blue ', ' green '); Console.log (colors.topopedstring ()); // Red|blue|green

What does this piece of code mean?

1. First, create a constructor function Specarr

2. An array object is then created inside the constructor

3. Also give this array object to impersonate with the Apply object, and then add some elements of the array (call-time assignment) (because the constructor here does not specify the number of parameters, so use apply, passed to the arguments object)

4. Then adds a method to the array object that uses the "|" Symbols combine arrays into a single string

5. Finally return to this object

So in the final analysis, when we need to add a custom method to a native object, using such as Array.prototype.xxx is not allowed, so this way, you can simply add a private method to the built-in object instantiated by the constructor, which is only visible to the instantiated object

var a=[' 456 ', ' fsdf ', ' Dasfgg '];a.topopedstring (); Console.log (a); // Error:uncaught typeerror:a.topopedstring is not a function

This method cannot be called by other array objects that are not instantiated by this constructor

It is important to note that objects created using this method can no longer use the instanceof operator to determine the type of object (colors instanceof array is true in this example, and colors instanceof Specarr is False)

In general, this method is used sparingly, unless the previous four methods are not sufficient to use this method

The second (secure constructor mode)

This method was invented by the famous Douglas Kest Rockford (Douglas Crockford, the author of the Butterfly book)

This approach is appropriate in some secure environments where this and new may be prohibited

The safe constructor pattern is a bit like the parasitic constructor pattern above, give me a chestnut.

function Person (name,age) {  var o=New  Object ();  Here you can define some private properties and methods   o.sayname=function() {    console.log (name);  }   return o;}
var Person_01=person ("Sakura", 22);
Person_01.sayname ();

In this case, you will find that you have not called the constructor with the new keyword, but instead called the constructor directly, so person_01 cannot invoke its properties directly (Name,age, etc.), only through Sayname to access his properties. This makes it ideal for running in certain secure execution environments

Well, there are so many ways to encapsulate objects (6 kinds), anyway, there are pros and cons, the last two can be right when understanding, next will introduce object-oriented second major features

Cond

...

JavaScript Learning Summary-creating objects (6_ other methods)

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.