开发者

Is javascript an object oriented language?

I have been learning javascript for some time. The book I read says that Javascript is a functional interpreted language. But many other resources I came across said that Javascript is Object oriented. So which one exactly does JS belongs to ? Or it does开发者_Python百科 not really matter? Thanks


JavaScript does have objects. I would say it's a hybrid: interpreted, functional, object-oriented, and dynamic.

I think these characteristics are very important, indeed. They are what makes it a good language, one that's more important every day.


JavaScript isn't a traditional object-orientated language since there isn't a way to define a typical class. It uses prototypal inheritance instead.

There are ways to simulate traditional classes with frameworks like Prototype and MooTools although it actually isn't a native JavaScript construct.

In the end, it doesn't matter as long as you can write code that's easy-to-understand and maintainable. Check out resources like jQuery Fundamentals. Even though it's jQuery specific, chapters 2, 9, and 10 applies to all JavaScript developers.


Yes. You can use javascript as a traditional object oriented language:

//Animal Class
function Animal(_name){
    this.name = _name; 
}

Animal.prototype.sleep = function(){
   console.log('zzzz');
}

Animal.prototype.talk = function(){
   console.log('Hey! how\'s it going?');
}

// Dog Class
function Dog(_name){
   // call the parent constructor
   Animal.apply(this,arguments);
}

// extends the Animal prototype chain
Dog.prototype = new Animal();
Dog.constructor = Dog;

Dog.prototype.talk = function(){
   console.log('woof woof');
}

//MAIN 
var dog = new Dog('roofus');

dog.sleep(); //produces 'zzzzz'
dog.talk(); //produces 'woof woof'

This technique emulates the classical inheritance model and is often referred to as 'pseudo classical inheritance'

Javascript is built around prototypal inheritance. Meaning instead of extending classes you extend objects (prototypes) instead. You can also employ inheritance through this technique.

var Animal = (function(){

    //everything inside the closure 
    //above outside returned object acts as a private variable
    var private = "private variable";

    //everything returned is a public field of the object
    var self =  {};
    self.name = null;
    self.sleep = function(){
            console.log('zzzzz');
    };
    self.talk =  function(){
           console.log('hey how\s it going');
    };

    return self;

})();

var Dog = (function(){
   var self = Object.create(Animal);
   self.talk = function(){
      console.log('woof woof')
   };

})();


//MAIN 
var dog = Object.create(Dog);
dog.name = "roofus";

dog.sleep(); //produces 'zzzzz'
dog.talk(); //produces 'woof woof'

It's important to understand how/why this works. Object.create creates a new object and makes the hidden "prototype" (proto) of that object the argument you pass in. The way javascript works is essentially when you access a field of an object (primitive, method, object) the js engine loops through the properties defined on that object, if the property is found it is returned if not it checks the hidden prototype proto property of the object and does the same thing. It does this recursively until the entire prototype chain has been searched or something is returned.

Also it is important to remember OOP is much more than just inheritance. Important OOP constructs like composition and encapsulation are fundamental to writing well structured javascript.


JavaScript supports functional programming techniques but I do not believe that you could class it as a functional language per se. Increasingly these days (all major browsers?) it is not interpreted, either. So I guess you could say that it is not a "functional interpreted language".

If you are interested in functional programming in JavaScript, check out some of the answers to this question for more ideas and references: Javascript as a functional language


Depends on what you call an object oriented language. JS doesn't have (primitive) classes, but it has

  • Method dispatch with dot notation: x.method()
    • and therefore, Polymorphism
  • Inheritance (albeit prototypal)
  • Garbage collection
  • Everything is an object

Looks OO enough to me!


JavaScript is multi-paradigm; more like a hybrid sort of language. Some texts may tell you that JavaScript is Object Oriented while others will say it is a functional programming language.I can say it is a mix of both;

JavaScript is an object oriented language but supports functional programming language style of using functions. Actually JavaScript is not a class-based Object Oriented because it uses prototyphal inheritance.This means in JavaScript Inheritance occurs not through defining classes of objects rather the objects (prototype) themselves.

Yes, JavaScript has a "class" keyword but that doesn't make it a class based object oriented programming language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜