Skip to content

Instantly share code, notes, and snippets.

@charlesr1971
Created February 12, 2016 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesr1971/08347a6556db00efc172 to your computer and use it in GitHub Desktop.
Save charlesr1971/08347a6556db00efc172 to your computer and use it in GitHub Desktop.
class-prototype-extended
var Animal = Class.create(Abstract,{
initialize: function(name,activity) {
this.name = name; //public member
this.activity = activity; //public member
}, //constructor method
say: function(message) {
this.something = function (something){return something;} //public method
return this.name + ': ' + message;
}, //public method
does: function(message) {
var adj = ""; //private member
var that = this; //private member allows private methods to read the 'this' scope
function addAdj(activity){
if(activity=="eats"){
adj = " for lunch";
}
console.log(that.activity);
return adj;
} //private method
this.adj = function (){return addAdj(this.activity);} //public method using privileges
return this.name + ': ' + this.activity + ': ' + message + this.adj();
}, //public method
sleep: function(message) {
return this.name + ': ' + message;
}, //public method
speak: function() {
console.log(this.name + " says: " + this.activity + "!");
}
});
var Dog = Class.create(Animal, {
say: function($super, message) {
return $super(message) + ', grrr!';
} //public method
});
var Wolf = Class.create(Animal, {
say: function($super, message) {
return $super(message) + ', owoooo!';
} //public method
});
var Cat = Class.create(Animal, {
say: function($super, message) {
return $super(message) + ', purrrr!';
}, //public method
does: function($super, message) {
return $super(message);
} //public method
});
var Owl = Class.create(Animal, {
initialize: function($super,name) {
$super(name); //inherited public members
var _privatemember = "private member"; //private member
this.haswings = true; //public member
this.getPrivateMember = function() {
return _privatemember;
} //private method
}, //constructor method
say: function($super, message) {
return $super(message) + ', terwooo!';
}, //public method
sleep: function(message) {
return this.name + ': ' + message + ', twit terwooo!';
}, //public method
closure: function(message) {
var string = "My name is "; //private member for use inside private methods
var that = this; //private member reads public scope for use inside private methods
function LastName(lastname){
return that.getPrivateMember() + ": " + string + message + " " + lastname;
} //private method
return LastName;
}
});
var Snake = Class.create(Animal, {
initialize: function($super,name) {
$super(name,"hssssss"); // mix-in members
} //constructor method
});
var animal = new Animal('Animal'); //constructor
console.log(animal.say('noise')); //public method call
var fido = new Dog('Fido'); //constructor
console.log(fido.say('woof')); //public method call inherit & add
var silver = new Wolf('Silver'); //constructor
console.log(silver.say('howl')); //public method call inherit & add
var mittens = new Cat('Mittens','eats'); //constructor
console.log(mittens.say('meowww')); //public method call inherit & add
console.log(mittens.does('fish')); //public method call inherit
console.log(mittens.adj()); //public method call using privileges
console.log(mittens.something("something")); //public method call
var mrhoot = new Owl('Mr Hoot'); //constructor
console.log(mrhoot.say('twit twit')); //public method call inherit & add
console.log(mrhoot.sleep('sleeps at night')); //public method call overide
var myName = mrhoot.closure("Charles"); //public method call closure access
console.log(myName("Robertson")); //public method call closure access finish
//var init = mrhoot.initialize(); //cannot access special initialize method
//console.log(init.getPrivateMember()); //cannot access special initialize private method
var ringneck = new Snake("Cedric"); //constructor
ringneck.speak(); //public method call inherit
Snake.addMethods({
speak: function($super) {
$super(); // inherit method
console.log("You should probably run. He looks really mad."); //add to method
}
});
ringneck.speak(); //public method call inherit & add
<script src="http://www.classroom.me.uk/assets/core/js/prototype-1.7.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment