JavaScript authoritative Guide Notes (9th chapter classes and modules)

Source: Internet
Author: User

1. Factory function

functionrange (from, to) {varR =inherit (range.methods); R.from=From ; R.to=to ; returnR;}; Range.methods={includes:function(x) {return  This. from <= x && x <= This. to; }, foreach:function(f) { for(varx = Math.ceil ( This. from); X <= This. to; X + +) f (x); }, ToString:function () {        return"(" + This. from + "..." + This. to + ")"; }}//Here is example uses of a Range object.varR = Range (1, 3);//Create a Range objectR.includes (2);//= True:2 is in the rangeR.foreach (Console.log);//Prints 1 2 3Console.log (R);//Prints (1...3)

2. Use constructors instead of factory functions: Note that you must use the new operator when calling

functionRange (from, to) { This. from =From ;  This. to =to ;} Range.prototype={includes:function(x) {return  This. from <= x && x <= This. to; }, foreach:function(f) { for(varx = Math.ceil ( This. from); X <= This. to; X + +) f (x); }, ToString:function () {        return"(" + This. from + "..." + This. to + ")"; } };//Here is example uses of a Range objectvarR =NewRange (1, 3);//Create a Range objectR.includes (2);//= True:2 is in the rangeR.foreach (Console.log);//Prints 1 2 3Console.log (R);//Prints (1...3)

3. Constructor properties

varF =function() {};//This is a function object.varp = f.prototype;//This is the prototype object associated with it.varc = p.constructor;//The the function associated with the prototype.c = = = F;//= = True:f.prototype.constructor==f for any functionvaro =NewF ();//Create An object o of Class FO.constructor = = = F;//= True:the Constructor property specifies the class

4, compare the following two paragraphs of the different code:

Range.prototype ={constructor:range,//explicitly set the constructor back-referenceIncludes:function(x) {return  This. from <= x && x <= This. to; }, foreach:function(f) { for(varx = Math.ceil ( This. from); X <= This. to; X + +) f (x); }, ToString:function () {        return"(" + This. from + "..." + This. to + ")"; }};/*a predefined prototype object that includes the constructor property*/Range.prototype.includes=function(x) {return  This. from <= x && x <= This. to;}; Range.prototype.foreach=function(f) { for(varx = Math.ceil ( This. from); X <= This. to; X + +) f (x);}; Range.prototype.toString=function() {R

JavaScript authoritative Guide Notes (9th chapter classes and modules)

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.