Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] Re: ES5 and constructors

Expand Messages
  • Morgaut Alexandre Louis Marc
    A good exemple var s = String(3); // typeof s === string var s = new String(3); // typeof s === object String() can be used to convert any value into a
    Message 1 of 7 , Nov 9, 2010
    • 0 Attachment
      A good exemple

      var s = String(3); // typeof s === "string"

      var s = new String(3); // typeof s === "object"

      String() can be used to convert any value into a string while new String() will create an object which constructor is String and which toString method will return its conversion into a string value.

      In ES5 strict mode, without new, when not applied as a method on an object, the value of this can null or undefined

      You can never say for sure that a function won't be used as a constructor

      You just have some little tricks like...
      - the function has been called once with new (then you are quite sure)
      - a property has been defined on its prototype (useless on other function than constructors)
      - its first letter is upper case (convention only)
      - it has a JSDoc with the tag "@constructor" (the developers probably wants you to manage it as a constructor)
      - it is a standard builtin constructor (from JS Core, DOM, BOM, ...)

      >



      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.