灭灭对《Effective JavaScript》的笔记(3)

Effective JavaScript
  • 书名: Effective JavaScript
  • 作者: David Herman
  • 副标题: 68 Specific Ways to Harness the Power of JavaScript
  • 页数: 240
  • 出版社: Addison-Wesley Professional
  • 出版年: 2012-12-6
  • Item 14: Beeare of Unportable Scoping of Named Function Expressions
    The specification mistake, which existed through ES3, was that JavaScript engines were required to represent the scope of a named function expression as an object, much like the problematic with construct.
    This program looks like it should produce null, but it actually produces a new object, because the named function expression inherits Object.prototype.constructor (i.e., the Object constructor function) in its scope. And just like with, the scope is affected by dynamic changes to Object.prototype. One part of a program could add or delete properties to Object.prototype and variables within named function expressions everywhere would be affected.
    更清晰的例子在这里:
    http://kangax.github.io/nfe/#spidermonkey-peculiarity
    2013-12-26 19:16:37 回应
  • 3. Working with Functions
    ## Item 16: Avoid Creating Local Variables with eval
    `eval` will pollute the caller's scope
    ## Item 17: Prefer Indirect eval to Direct eval
    The other kind of call to eval is considered “indirect,” and evaluates its argument in global scope.
    eval 内的 scope 会变成 global, 这样 local scope 的东西无法访问
    A concise way to write an indirect call to eval is to use the expression sequencing operator (,) with an apparently pointless number literal:
    (0,eval)(src);
    
    ## Item 24: Use a Variable to Save a Reference to arguments
    a new arguments variable is implicitly bound in the body of each function.a The arguments object we are interested in is the one associated with the values function, but the iterator’s next method contains its own arguments variable. So when we return arguments[i++], we are accessing an argument of it.next instead of one of the arguments of values.
    ## Item 28: Avoid Relying on the toString Method of Functions
    JavaScript engines are not required to produce accurate reflections of function source code via `toString`
    Try: `func.bind({}).toString()`
    Different engines may produce different results from `toString`
    ## Item 29: Avoid Nonstandard Stack Inspection Properties
    Do not use `arguments`, `function.caller` as call stack
    2014-01-01 00:01:05 回应
  • 4 - 7
    ## Item 40: Avoid Inheriting from Standard Classes
    function Dir(){}
    
    Dir.prototype = Object.create(Array.prototype)
    
    var dir = new Dir('1', '2')
    
    dir.length // => 0
    
    Since `[[Class]]` of `Array` is `Array`
    `[[Class]]` of `Dir` is `Object`
    ## Item 45: Use hasOwnProperty to Protext Against Prototype Pollution
    '__proto__' in {} // Not defined.
    
    ## Item 58: Distinguish between Array and Array-Like
    Overloading two types means there must be a way to distinguish the cases. And it’s not possible to detect that a value implements a structural interface. This leads to the following rule: APIs should never overload structural types with other overlapping types
    2014-01-01 18:05:52 回应