Classes and objects in JavaScript seem super weird... Started learning JavaScript a couple days ago, just hit objects. Does javascript have classes or does it not? I have no idea, and the notation is super bizarre coming from a C++, Java, and Python background.
Codeacademy as well heh. Was all gravy until it hit objects, then it lost me. Was just like "Alright and here's objects and it's weird notation... Moving on!"
Currently reading other sources to figure out what the hell an object really is in Javascript
And what the hell, can you just add new fields to an object on the fly?
I just chose one way and stuck with it. I'm not yet too interested in delving deeply into how javascript works, I just figure out how to do what I want with it and then do it.
Javascript classes are quite weird in my opinion and there are different ways (Patterns) to accomplish them.
For example something like this will give you instantiation, encapsulation and inheritance but the variable will be static.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var Foo = (function() {
// "private" variables
var _bar;
// constructor
function Foo(){};
// add the methods to the prototype so that all of the
// Foo instances can access the private static
Foo.prototype.getBar = function() {
return _bar;
};
Foo.prototype.setBar = function(bar) {
_bar = bar;
};
return Foo;
})();
If you don't want the variable to be static you could do something like this instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var Foo = (function() {
// constructor
function Foo(){
this._bar = "some value";
};
// add the methods to the prototype so that all of the
// Foo instances can access the private static
Foo.prototype.getStaticBar = function() {
returnthis._bar;
};
Foo.prototype.setStaticBar = function(bar) {
this._bar = bar;
};
return Foo;
})();
We lose encapsulation in this example but it is common to see private variables prefixed with a underscore and most programmers will know not to alter it since it is meant for internal use.
I borrowed the code from this post http://stackoverflow.com/questions/12610394/javascript-classes since didn't feel like typing up my own. So that thread might contain more information on the different patterns availible to making classes in JS.
Yeah, JS OOP takes a little getting used to. At least CodeCademy holds you hand just enough to get the hang of it. Like the Cash Regsiter example for the Intro to OOP II section (though, I'll admit, somethings I blanked at times and had to look back):
function StaffMember(name,discountPercent){
this.name = name;
this.discountPercent = discountPercent;
}
var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);
// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("BHXSpecter", 20);
var cashRegister = {
total:0,
lastTransactionAmount: 0,
add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},
scan: function(item,quantity){
switch (item){
case"eggs": this.add(0.98 * quantity); break;
case"milk": this.add(1.23 * quantity); break;
case"magazine": this.add(4.99 * quantity); break;
case"chocolate": this.add(0.45 * quantity); break;
}
returntrue;
},
voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
// Create a new method applyStaffDiscount here
applyStaffDiscount: function(employee){
this.total -= (this.total * (employee.discountPercent / 100));
}
};
cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object
// to applyStaffDiscount
cashRegister.applyStaffDiscount(me);
// Show the total bill
console.log('Your bill is '+cashRegister.total.toFixed(2));
[EDIT] I realize I said it felt natural to me, but I got to thinking back and it only felt natural after I finished CA and dabbled with a few things in JS. jQuery was a fun API to learn if you are interested in Web Dev.
holy crap now i understand why js scares a lot of people. that is some of the weirdest oop i have ever seen. ill stick to php. the weirdest oop i can get used to is python, because it doesnt have a type system (or at least not one for variables) and it has an init() constructor
i like python just because it reminds me a lot of c. if you can see it like that, its actually easy, but its not good for huge projects is the only thing. i mainly use it just for quick hacks or temp solutions to problems such as a nightly download manager.
I guess that's why I can't get into Python - I have an extremely strong aversion to C and anything like C. Thankfully C++ is so incredibly different from C that I don't even consider them related in my mind.
c++ is my favorite and my main too, but i like to use c (and brainfuck for that reason i guess) is to keep my mind sharp. i programmed in only c++ for a long time, and took a lot for granted, like prototyping, multiple inheritance, and containers so it just keeps things in perspective. (you dont realize how amazing basic_string<char> is until you have only had access to char **'s
I don't understand the fear of dynamic codeing. For me js was easy to learn. A variable is a variable and a function is a function. You never had to worry about types.
But I was once new to C++. It was very hard at first to grasp the concept of types, when I was spoiled with dynamic code. If I wasn't so set on learning game dev. I would have walked away from C++. Now C++ is my baby! I raise a glass to trying new things.
It takes all my strenght to stop myself from leaping into opengl just yet. I need to do a little more with 2D first, then 3D here I come!!