Skip to content

Instantly share code, notes, and snippets.

@ychaouche
Created July 26, 2012 11:42
Show Gist options
  • Save ychaouche/3181595 to your computer and use it in GitHub Desktop.
Save ychaouche/3181595 to your computer and use it in GitHub Desktop.
javascript / C++
Do you agree what that statement below ?
"In Javascript, it is preferable to declare methods outside of the function definition.
Because if you define it inside the function, it's like you're defining a method inside a constructor.
You usually don't do that do you ? usually a constructor just initialises attributes.
You would declare the methods outside of the function definition."
So instead of this :
var Dog = function(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
Dog.prototype.bark = function(){console.debug("arf !");};
}
You write this
var Dog = function(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Dog.prototype.bark = function(){console.debug("arf !");};
Isn't this a little bit like in C++ ? where methods are defined outside of the the class definition ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment