SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A "JavaScript: The Good Parts" question please?

    Hi
    for a very long time now I have been wanting to learn JavaScript better than I have. So I was delighted when I found out there was a new book "JavaScript: The Good Parts" which has gotten excellent reviews. I obtained an e-book version of this book and have been reading it. The e-book version affords me the ability to cut and paste the code from the pages and try it. I have been doing so sometimes needing to make small corrections in the author's code. This has not been a problem until now. I have some code from the author which needs correcting and I am stuck. Let me quote the passage in the e-book and show you the error the Firebug Firefox extension points out.
    4.7. Augmenting Types
    JavaScript allows the basic types of the language to be augmented. In Chapter 3 <objects.html>, we saw that adding a method to Object.prototype makes that method available to all objects. This also works for functions, arrays, strings, numbers, regular expressions, and booleans.
    For example, by augmenting Function.prototype, we can make a method available to all functions:
    Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
    };
    By augmenting Function.prototype with a method method, we no longer have to type the name of the prototype property. That bit of ugliness can now be hidden.
    JavaScript does not have a separate integer type, so it is sometimes necessary to extract just the integer part of a number. The method JavaScript provides to do that is ugly. We can fix it by adding an integer method to Number.prototype. It uses either Math.ceiling or Math.floor, depending on the sign of the number:
    Number.method('integer', function ( ) {
    return Math[this < 0 ? 'ceiling' : 'floor'](this);
    });

    document.writeln((-10 / 3).integer( )); // -3
    The firebug error this yields is.
    Math[this < 0 ? "ceiling" : "floor"] is not a function
    (?)()program.js (line 6)
    program.js()program.js (line 9)
    [Break on this error] return Math[this < 0 ? 'ceiling' : 'floor'](this);
    If you could tell me what causes this that would be great.
    The code the exact way I pasted it into my editor and ran it in Firefox is below.
    Code:
    Function.prototype.method = function (name, func) {
        this.prototype[name] = func;
        return this;
    };
    Number.method('integer', function () {
        return Math[this < 0 ? 'ceiling' : 'floor'](this);
    });
    
    document.writeln((-10 / 3).integer());  // -3

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript's ceiling function is Math.ceil, so replace 'ceiling' with 'ceil'.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The publishers web site has an errata for Javascript: The Good Parts
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r51 thanks you found it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •