Define the method on the prototype object instead of rewriting it with the new object. Overrides make inheritance impossible: Resetting the prototype overrides the entire base class.
function Jedi () {console.log (' new Jedi ');} Badjedi.prototype = {Fight:function fight () {Console.log (' fighting ');},block:function block () {Console.log (' Blocking ');}};/ /goodJedi.prototype.fight = function fight () {Console.log (' fighting ');}; Jedi.prototype.block = function block () {console.log (' blocking ');}; <strong></strong>
Prototype in JavaScript can be found in the http://www.w3school.com.cn/jsref/jsref_prototype_array.asp
Method should return this, which facilitates the formation of a method chain
BadJedi.prototype.jump = function () {this.jumping = True;return true;}; Jedi.prototype.setHeight = function (height) {this.height = height;}; var luke = new Jedi (); Luke.jump (); = Trueluke.setheight (20); = = undefined//GoodJedi.prototype.jump = function () {this.jumping = True;return this;}; Jedi.prototype.setHeight = function (height) {this.height = Height;return this;}; var luke = new Jedi (); Luke.jump (). SetHeight (20);
writing a custom ToString () method is OK, just make sure it works and doesn't have side effects
function Jedi (options) {Options | | (options = {}); this.name = Options.name | | ' No Name ';} Jedi.prototype.getName = function GetName () {return this.name;}; Jedi.prototype.toString = function toString () {return ' Jedi-' + This.getname ();};
Genesis 1:17 and God set them in the firmament of the heaven-give light upon the Earth.
"Notes" JavaScript coding specification-constructors