javascript - 用什么代替 JavaScript 中的类?

标签 javascript ecmascript-6

<分区>

背景

我是一名开发人员,在 Java 和 C# 方面拥有大约 9 年的背景历史。

在那些语言中,Classes 和taxonomic 分类以及固有的对象范式。

所以,当ECMA6出来后,我很高兴看到这门语言的类(class)……我开始到处使用它们。

问题

事实证明,在 JavaScript 中使用类是一个陷阱。

If you use them, you will go to your grave never knowing how miserable you are.

而且您永远不会真正理解 JavaScript。你会认为你这样做,但你没有。

问题

很明显,看完整个 session 后,我意识到我不懂 JavaScript

我一生都被 OOP with classes 范式格式化,现在我什至不知道去哪里寻求帮助,甚至不知道从哪里开始。

  1. 在 JavaScript 风格中,您将如何通过继承来表示动物王国?例如,我会使用 Animal 类,然后是 Dog 类,然后实例化狗的对象。这不是 JavaScript 的方式。

一个非JavaScvript的例子:

class Animal{
    constructor(aName){
        this.name = aName;
    }
}

class Dog extends Animal{
    constructor(){
        super(aName);
        this.type = "Furry dog";
    }
}

let myDog = new Dog("Boby");
  1. 执行此操作的 JavaScript 方法是什么?

此时,我正在寻求指导。尝试后我找不到任何有用的东西,主要是因为我相信我迷路了,我什至没有在寻找正确的东西。

提前致谢。

最佳答案

据我所知,JavaScript 是一种基于原型(prototype)的语言,具有一流的功能,您可以阅读 here .

现在。实际上这只是 OPP 的一种风格,这意味着您将工作和思考对象和继承。你只需要知道,在 JavaScript 中,没有类,但我们有原型(prototype)。但要记住。一切都与功能有关。

对象构造函数

要创建您自己的对象定义,您可以执行以下操作:

function Animal() {
    this.name = null;
    this.age = 0;
    this.genus = null;
    this.isWild = true;
};

Animal.prototype.hasName = function() {
    return this.name !== null;
};

Animal.prototype.isItWild = function() {
    return this.isWild;
};

// Here we create an instance of Animal
var someAnimal = new Animal();
// Let's give it a name
someAnimal.name = 'Dobby';
console.log('Hey there. My pet name is %s', someAnimal.name);
console.log('is it wild? %o', someAnimal.isWild);

现在您有了 Animal 原型(prototype)。让我们看看如何扩展它的属性来创建 Dog 原型(prototype):

function Dog() {
    this.genus = 'Canis';
    this.race = 'unknown';
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
    console.log('rrouff!');
};

现在让我们创建一个狗的比赛:

function SiberianHusky(name, age) {
    this.name = name;
    this.age = age;
    this.race = 'Siberian Husky';
}

SiberianHusky.prototype = Object.create(Dog.prototype);
SiberianHusky.prototype.constructor = SiberianHusky;

// Let's create our Siberian Husky
var myPet = new SiberianHusky('Bobby', 5);
// Aww. It is barking!
myPet.bark();

原型(prototype)对象

您可以在示例中看到每个定义都使用 the Object prototype .那是用来定义一个对象是怎样的。您可以将其视为类定义。

如何避免过度使用原型(prototype)属性

您可以执行以下操作,而不是每次都写入原型(prototype):

Animal.prototype = {
    name: null,
    age: 0,
    genus: null,
    isWild: true,
    hasName: function() { ... }
    isItWild: function() { ... }
}

要全面了解原型(prototype)的概念以及它们如何相互继承,您可以查看this .

最后的笔记

JavaScript 是一种多范式语言。您可以使用 Object Oriented Programming Paradigm , Imperative Programming ParadigmFunctional Programming Paradigm ,这意味着您可以用很多不同的方式对同一个应用程序进行编程。

一些有用的链接

https://en.wikipedia.org/wiki/JavaScript

https://en.wikipedia.org/wiki/Prototype-based_programming

干杯。

关于javascript - 用什么代替 JavaScript 中的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42019732/

相关文章:

javascript - 为什么西里尔字母的正则表达式会漏掉一个字母?

javascript - 如何在 ASP.NET 文本框中强制输入大写?

javascript - 可在移动设备上关闭所有部分的 Accordion ,但在平板电脑和台式机上始终打开一个 Accordion 部分

javascript - 为什么动态加载确实存在的文件时会收到 404 错误?

javascript - 如何从 onclick 调用 ES6 模块函数

javascript - ES6 : Can i init variable with value of the result of destruction of an object?

javascript - 下划线 : Find the most frequently occurring object in an array?

javascript - 清除/停止javascript中的滚动惯性?

javascript - 如何 Promisify 具有多个回调参数的函数?

javascript - 在 Jquery 函数上使用箭头函数