Reputation: 1196
I somewhat understand the difference between Javascript and Java when it comes to OOP. I read the bottom paragraph in the Javascript book that I have but I don't fully understand what it mean, can you further explain and provide examples to show the difference between the two languages?
"In many object-oriented programming languages, it is possible to define a class of objects and then create individual objects that are instances of that class."
Thanks!
Upvotes: 2
Views: 4688
Reputation: 51030
In Java you can create a class using the class
keyword. A class is the blueprint for its instances (objects).
e.g.
class AClass { // AClass is a class
AClass() { } //this is the constructor which will be used to create instances of this class
}
And then create and instance of the class:
AClass obj1 = new AClass(); // an instance of the class AClass
AClass obj2 = new AClasss(); //another instance of the class AClass
So, every object in Java is an instance of some class. And you can have many different instances of a class, but you can't have any object without class which acts as their blueprint.
And, in the class you can define member variables and methods which can either belong to the class itself, which will be shared by all of it's instances, or belong to the instance in which case every instance will have their own copy.
In Javascript I mostly create objects as follows:
var obj = { };
So, I don't need to create any class to actually create an object.
Upvotes: 3
Reputation: 112867
What most people do is use constructor functions, like so:
function MyClass(a, b) {
// Private instance properties
var x = "secret";
var y = "also secret";
// Public instance properties
this.a = a;
this.b = b;
// "Privileged" methods; can access private and public properties
this.foo = function () {
return x + " " + this.a;
};
}
// "Public" methods; cannot access private properties
MyClass.prototype.bar = function () {
return this.a + " " + this.b;
};
// Public shared properties; not recommended:
MyClass.prototype.w = "stuff";
// Static methods
MyClass.baz = function () {
return "i am useless";
};
You would then use this constructor function like so:
var instance = new MyClass("hi", "Meen");
asssert.equal(instance.foo(), "secret hi");
assert.equal(instance.bar(), "hi Meen");
assert.equal(MyClass.baz(), "i am useless");
var also = new MyClass("hi", "Raynos");
instance.w = "more stuff";
assert.equal(also.w, "more stuff");
If you wanted to do inheritance, you would do something like:
function Inherited(a) {
// apply parent constructor function
MyClass.call(this, a, "Domenic");
}
Inherited.prototype = Object.create(MyClass.prototype);
var inherited = new Inherited("hello there good chap");
assert.equal(inherited.a, "hello there good chap");
assert.equal(inherited.foo(), "secret hello there good chap");
assert.equal(inherited.bar(), "hello there good chap Domenic");
Upvotes: 9
Reputation: 169471
Object oriented javascript is very simple.
You create an object.
var Klass = {
method: function () {
},
constructor: function() {
},
...
};
And then you create an instance of that object through prototypical inheritance
// instantiate
var o = Object.create(Klass);
// initialize
o.constructor();
This is different from classical OO because any object can be a "class" in javascript. and there is only one inheritance chain, the chain of prototypes.
Normally in classical OO languages the classes would inherit from each other and the instances would have a seperate chain of inheritance.
Also in classical OO you have two objects, the actual class object and the blueprint for the instance (i.e. static
). This doesn't exist in javascript because there is only one object, the blueprint.
Disclaimer: Object.create
is ES5, so you need an es5-shim for legacy platforms.
Upvotes: 4