Last active
August 29, 2015 14:04
-
-
Save jeffreyguenther/04f76c614d9b18c28d23 to your computer and use it in GitHub Desktop.
Class and Instance Methods and Variables example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class @Sales # attach the class to the global namespace so it can be accessed. | |
price: 5 | |
@units: 100 | |
@greatjob: (name) -> | |
"Great job #{name}. You sold #{@units} for #{Tester::price}" | |
### | |
@greatjob is a class method. This means it is only accessible as in | |
Tester.greatjob("Jeff") or `instance`.constructor.greatjob("jeff") | |
name is local to the method, so you have access to that | |
To access @units, you access it with @units, or this.units. | |
To access the instance value, you use Tester::price, which is | |
short for Tester.prototype.price. This is necessary because you | |
in a static context and it's access the prototypial value. My understanding | |
of what a prototype is it is an instance that as always hanging around | |
and that can be copied to create an other instance. | |
### | |
price_change: (price) -> | |
"#{@price} is raise to #{price}\n#{@constructor.greatjob("Tom")}" | |
### | |
This is an instance method. You can access it with s = new Sales; s.price_change(1000) | |
You can access the class variable using @price. | |
You can access the local price variable directly | |
You can access the class method by referring to `this` object's constuctor and then calling the method | |
### | |
sell: => | |
"I will sell #{Tester.units} for #{@price} then I will be praised #{@constructor.greatjob("Jeff")}" | |
### | |
This method is almost the same as above. The only difference is that by using => it guarantees if you use | |
`this` inside the method it will refer to the instance. | |
### | |
buy_donuts = (number) -> | |
"#{number} donuts!" | |
### | |
Buy is a local method that cannot be access from outside the object in any way. It is like a private method. | |
### | |
donuts = 10 | |
# local, private variable only usable inside Sales | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
this.Sales = (function() { | |
var buy_donuts, donuts; | |
function Sales() { | |
this.sell = __bind(this.sell, this); | |
} | |
Sales.prototype.price = 5; | |
Sales.units = 100; | |
Sales.greatjob = function(name) { | |
return "Great job " + name + ". You sold " + this.units + " for " + Tester.prototype.price; | |
}; | |
/* | |
@greatjob is a class method. This means it is only accessible as in | |
Tester.greatjob("Jeff") or `instance`.constructor.greatjob("jeff") | |
name is local to the method, so you have access to that | |
To access @units, you access it with @units, or this.units. | |
To access the instance value, you use Tester::price, which is | |
short for Tester.prototype.price. This is necessary because you | |
in a static context and it's access the prototypial value. My understanding | |
of what a prototype is it is an instance that as always hanging around | |
and that can be copied to create an other instance. | |
*/ | |
Sales.prototype.price_change = function(price) { | |
return "" + this.price + " is raise to " + price + "\n" + (this.constructor.greatjob("Tom")); | |
}; | |
/* | |
This is an instance method. You can access it with s = new Sales; s.price_change(1000) | |
You can access the class variable using @price. | |
You can access the local price variable directly | |
You can access the class method by referring to `this` object's constuctor and then calling the method | |
*/ | |
Sales.prototype.sell = function() { | |
return "I will sell " + Tester.units + " for " + this.price + " then I will be praised " + (this.constructor.greatjob("Jeff")); | |
}; | |
/* | |
This method is almost the same as above. The only difference is that by using => it guarantees if you use | |
`this` inside the method it will refer to the instance. | |
*/ | |
buy_donuts = function(number) { | |
return "" + number + " donuts!"; | |
}; | |
/* | |
Buy is a local method that cannot be access from outside the object in any way. It is like a private method. | |
*/ | |
donuts = 10; | |
return Sales; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment