06.07.2017 Views

Mastering JavaScript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object-Oriented <strong>JavaScript</strong><br />

}<br />

// Privileged Methods<br />

// Can be invoked from outside and can access private members<br />

// Can be replaced with public counterparts<br />

this.book=function(){<br />

if (!isAvailable()){<br />

this.available=false;<br />

} else {<br />

console.log("Player is unavailable");<br />

}<br />

};<br />

this.getSport=function(){ return playerSport; };<br />

// Public properties, modifiable from anywhere<br />

this.batPreference="Lefty";<br />

this.hasCelebGirlfriend=false;<br />

this.endorses="Super Brand";<br />

// Public methods - can be read or written by anyone<br />

// Can only access public and prototype properties<br />

Player.prototype.switchHands = function(){ this.<br />

batPreference="righty"; };<br />

Player.prototype.dateCeleb = function(){ this.hasCelebGirlfriend=true;<br />

} ;<br />

Player.prototype.fixEyes = function(){ this.wearGlasses=false; };<br />

// Prototype Properties - can be read or written by anyone (or<br />

overridden)<br />

Player.prototype.wearsGlasses=true;<br />

// Static Properties - anyone can read or write<br />

Player.noOfPlayers = 0;<br />

(function PlayerTest(){<br />

//New instance of the Player object created.<br />

var cricketer=new Player("Vivian","Cricket",23,"England");<br />

var golfer =new Player("Pete","Golf",32,"USA");<br />

console.log("So far there are " + Player.noOfPlayers + " in the<br />

guild");<br />

[ 108 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!