The function of the essence of JavaScript language

Source: Internet
Author: User

Write in front

See a lot of book reviews and reading notes all say "JavaScript language essence" Abas, reputation. Of course, you can understand.

In fact, personally think that the functional part is not very good, the example is not very appropriate, before you can not understand because it was successfully misled, like "Head First" design Model chapter "strategy mode", the author is somewhat off the chapter theme, the reader easily misleading

Disclaimer: The function that is given to create the object is called the "creation function" , in order to be separated from the "constructor" area. It's not very nice.

I. Points to note in the source code

It is easy to get the source code, and the Chinese version of the book on the same page, looked carefully over the discovery of a very subtle place, of course, not very good understanding

P.S. Source a little bit of a problem: "Creative function" cat curly braces mismatch, Chinese version 54 pages, return that; less before};

The prototype function of Object superior has a very subtle place inside.
Object.Method (' Superior ', function (name) {    var = this,        method = That[name];    return function (  ) {        return method.apply (that, arguments);    };});

The highlight is the last sentence of the arguments, seemingly unintentional, in fact, is intentional, indicating that the use of superior call the parent class method can also be passed the parameter. Of course, you need to modify the calling method, the test code is as follows:

Function.prototype.method = function (name, func) {this.prototype[name] = func; return this;};    var mammal = function (spec) {var that = {};    That.get_name = function () {return spec.name;    }; That.says = function () {return spec.saying | |    ‘‘;    }; return that;}; var cat = function (spec) {spec.saying = Spec.saying | |    ' Meow ';    var that = mammal (spec);        That.purr = function (n) {var i, s = ';            for (i = 0; i < n; i + = 1) {if (s) {s + = '-';        } s + = ' r ';    } return s;    }; That.get_name = function () {alert ("Cat.get_name:" + arguments.length);///Return That.says () + "+    Spec.name + "+ that.says () +" ["+ Arguments.length +"] ";    }; Return That;};o    Bject.method (' Superior ', function (name) {var = this, method = That[name];     return function () {alert ("Superior:" + arguments.length);   Return method.apply (that, arguments); };});    var Coolcat = function (spec) {var that = Cat (spec), Super_get_name = That.superior (' get_name '); That.get_name = function () {alert ("Coolcat.get_name:" + arguments.length);///return ' like ' + super_get_    Name.apply (this, arguments) + ' baby ';    }; return that;}; var mycoolcat = Coolcat ({name: ' Bix '}), var name = Mycoolcat.get_name (1, 2, 3);//' Like Meow Bix Meow Baby ' alert (nam    e); ' Like Meow Bix meow[3] baby '

P.s. began to think that the last arguments superior function is the author's error, that should need to be the outside of the arguments object to method instead of inside, around a large circle found that he was wrong, daoxing enough, not the second to understand the meaning of Uncle Douglas.

Two. The original purpose of the function

The functional section begins with the original idea: To create a private property, the last mentioned "security object"

The goal is understandable, it is necessary to implement the private property. But the example mammal cat-and Coolcat is inappropriate, and the author wants to show that it can be inherited in a functional way

Of course, not strictly inherited, because the functional way does not use the custom type, the subclass instance and the parent class instance's is-a relationship also cannot discuss

P.S. Look at the first time cat's example took me to the ditch, thinking that the function is to abandon new, fully use the function to implement inheritance. Nature is in the ditch more and more go deeper

Three. The idea of function

Look directly at the code, the code will speak for itself:

/* Functional thought: * 1. Create Object * 2. Add private Property * 3. Public interface (add public attribute) * 4. Returns the Object *//* * method:getsuper * @param spec Specification object, providing the basic data required to create the object * @            Param the container for sharing data between my "Create function" */function getsuper (spec, my) {var obj; The object to return var my = my | |  {};  Create a//private property without passing in var attr = Spec.value;    Take data from Spec object var fun = function () {alert (attr);    }//[optional] load the data that you want to share with other creative functions into my//create object, in any way, such as new, literal, call other "create function" obj = {name: "Superobject"};    public interface obj.fun1 = fun; returns obj return obj;    /* * method:getsub * parameters ibid. */function getsub (spec, my) {var obj; var my = my | |    {};    Private attribute var attr = spec.value + 1;    var fun = function () {alert (attr);   }//[optional] Share//create Object obj = GetSuper (spec, my);    Can be directly passed, of course, can also be changed to re-pass, or preach something else//public interface obj.fun2 = fun; returns obj return obj; Test var spec = {Value:1};var Sub = getsub (spec); No incoming my,my should be used only between the "creative functions" sub.fun1 (); 1sub.fun2 (); 2

P.s. Again, "create object--enhanced--return new object" Isn't that what Nicholas called "module mode" invented by Douglas?

Four. Anti-Counterfeiting object (persistent object)

The core of the functional part is it, notice the way the interface is exposed in the example above:

Private attribute var myfun = function () {/* ... */};//public interface obj.fun = myfun;

Rather than directly using:

public interface Obj.fun = function () {/* ... */};

The first way is more secure , because even if the fun is modified from the outside, the internal other calls to the Myfun method can still work, such a function object is called anti-counterfeiting objects

Full definition:

The properties of the security object can be replaced or deleted, but the integrity of the object is not compromised

Also known as persistent objects, a persistent object is a collection of simple function functions

Something

The learning notes of the "JavaScript language Essentials" are now over, filling up the [functional] vacancy, and taking a look at the rest of the Notes Breeze: Learning Notes from the JavaScript language essence

The function of the essence of JavaScript language

Related Article

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.