javascript 是一种面向对象的语言吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5852583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:34:19  来源:igfitidea点击:

Is javascript an object oriented language?

javascript

提问by Codier

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 not really matter? Thanks

我已经学习 javascript 一段时间了。我读过的书说 Javascript 是一种函数式解释语言。但是我遇到的许多其他资源都说 Javascript 是面向对象的。那么JS到底属于哪一个呢?还是真的无所谓?谢谢

回答by duffymo

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

JavaScript确实有对象。我会说它是一种混合体:解释型、功能型、面向对象和动态型。

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

我认为这些特征确实非常重要。它们使它成为一种好语言,一种每天都变得更加重要的语言。

回答by cphoover

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

是的。您可以将 javascript 用作传统的面向对象语言:

//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.

Javascript 是围绕原型继承构建的。意思是不是扩展类,而是扩展对象(原型)。您还可以通过这种技术使用继承。

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 protoproperty of the object and does the same thing. It does this recursively until the entire prototype chain has been searched or something is returned.

了解这是如何/为什么起作用很重要。Object.create 创建一个新对象,并使该对象的隐藏“原型”(proto)成为您传入的参数。javascript 的工作方式本质上是当您访问对象(原始、方法、对象)的字段时,js 引擎循环遍历该对象上定义的属性,如果找到该属性,则返回它,否则它会检查对象的隐藏原型proto属性并执行相同的操作。它以递归方式执行此操作,直到搜索到整个原型链或返回某些内容为止。

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.

同样重要的是要记住 OOP 不仅仅是继承。重要的 OOP 结构(如组合和封装)是编写结构良好的 javascript 的基础。

回答by Dan

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

JavaScript 不是传统的面向对象语言,因为没有办法定义典型的类。它使用原型继承来代替。

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

有一些方法可以使用 Prototype 和 MooTools 等框架模拟传统类,尽管它实际上不是原生 JavaScript 构造。

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.

归根结底,只要你能写出易懂易维护的代码就行了。查看jQuery Fundamentals等资源。尽管它是特定于 jQuery 的,但第 2、9 和 10 章适用于所有 JavaScript 开发人员。

回答by hugomg

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

取决于你所说的面向对象语言。JS 没有(原始)类,但它有

  • Method dispatch with dot notation: x.method()
    • and therefore, Polymorphism
  • Inheritance (albeit prototypal)
  • Garbage collection
  • Everything is an object
  • 使用点符号的方法调度: x.method()
    • 因此,多态性
  • 继承(虽然是原型)
  • 垃圾收集
  • 一切都是对象

Looks OO enough to me!

对我来说已经足够了!

回答by Coxy

JavaScript supportsfunctional 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 nota "functional interpreted language".

JavaScript支持函数式编程技术,但我不相信您可以将其归类为函数式语言本身。如今(所有主要浏览器?)也越来越多地不被解释。所以我猜你可以说它不是“函数式解释语言”。

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

如果您对 JavaScript 中的函数式编程感兴趣,请查看此问题的一些答案以获取更多想法和参考:Javascript 作为函数式语言