Register now or log in to join your professional community.
yea javascript is object oriented but its not classless-based object oriented language its prototype-based
It is object oriented.
Example:
varcar = {brand: "Lamborghini", color: "purple", getCarInfo: function(){returnthis.color + '' + this.brand + ' car'; }}Then:
alert(car.getCarInfo());will popup the message: "purple Lamborguini car".
Javascript is object oriented, but its not based on classes for object orienting, but its a prototype based language
Obviously !
There are many way to create and use object in javascript.
Object literal :
var MyObject = {
foo: 'foo',
bar: function() { console.log(this.foo);}
}myObject = Object.create(MyObject);
myObject.bar(); // Using of my object
Constructor function :
function MyObject() {
this.foo = 'foo';
}
MyObject.prototype.bar = function() {
console.log(this.foo);
}
// Using of my object;
myObject = new MyObject();
myObject.bar();
EcmaScript 6 object :
class MyObject {
constructor() {
this.foo = 'foo';
}
bar() {
console.log(this.foo);
}
}
myObject = new MyObject();
myObject.bar();