Is it good practice to protect designers for the lack of "new"?

From the secrets of Javascript Ninja (great walkthrough by the way):

// We need to make sure that the new operator is always used
function User(first, last){ 
  if ( !(this instanceof User) ) 
    return new User(first, last); 

  this.name = first + " " + last; 
} 

var name = "Resig"; 
var user = User("John", name); 

assert( user, "This was defined correctly, even if it was by mistake." ); 
assert( name == "Resig", "The right name was maintained." );

Does the real-world code style guide do all designers perform this protection (first two lines)? I believe that linter will catch such challenges as User("John", "Resig"), and will warn about the missing new, right?

+5
source share
4 answers

I recommend using strict mode and a test in a browser that supports it. For instance,

function User(first, last) {
    "use strict";
    this.name = first + " " + last;
}

this will result in strict mode if you neglect new. For instance,

User("a", "b")

TypeError Firefox. instanceof. , . , this undefined . undefined TypeError.

+7

JSHint.com, , new:

function User(first, last){ 
  this.name = first + " " + last; 
} 

var name = "Resig"; 
var user = User("John", name); 

:

Line 2: this.name = first + " " + last;
Missing "use strict" statement.

Line 5: var name = "Resig";
Redefinition of 'name'.

.

, , , , new. linter , .

; , .

, - ; , JS new, , , ( , JS ), , , .

+2

, new, , User(x, y) new User(x, y).

, . (, ), , , ( , , , , , user).


( ) "" JavaScript, .

"" ( JS , DOM) : new MyComponent(domObject) - DOM ( ..)..

, JS-, : myTemplate(someData) - HTML. , , , , HTML.

HTML , "", new . , (data-component ), , HTML DOM, DOM , ( new ).

, .

+1

: new:

function User(first,last,id){
  User.Instance = User.Instance|| function(firstname,lastname,id){
      this.firstname = firstname || 'nofirstname';
      this.lastname = lastname || 'nolastname';
      this.id = id || 0;
      if (!User.Instance.prototype.nameUpper){
       User.Instance.prototype.nameUpper = function(){
                   return this.name.toUpperCase();
       };
       User.Instance.prototype.resetId = function(){
                   this.id = 0; return this;
       };
       User.Instance.prototype.toString = function(){
           return [this.id,': ',
                   this.firstname[0].toUpperCase(),
                   this.firstname.slice(1),
                   ' ',
                   this.lastname[0].toUpperCase(),
                   this.lastname.slice(1)].join('');
       };
      }
  }
  return new User.Instance(first,last,id);
}
//usage
var pete = User('pete','johanssen',1),
    jean = User('jean','harlowe',2),
    mary = new User('mary','wsnovsky',3);
console.log(pete); //=> 1: Pete Johanssen'
console.log(mary); //=> 3: Mary Wsnovsky'

: " " /. " " ? , , , . , , new, . : ? ? chukj , , ?

, javascript, - . JSLint, , - :

jslint

, , jslint , , " ", " " .. , jslint " " .

javascript " javascript-". , , ( "" ). . , . . , . classes .

: , , " ". : .

+1

All Articles