"Notes" JavaScript coding specification-constructors

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.