繁体   English   中英

Javascript中的类关键字

[英]Class keyword in Javascript

根据这篇文章,它应该是一个Javascript 2.0方法来定义类。 但是,我从未在实践中看到过这一点。 这样的问题。 如何使用class关键字和Javascript 1.x做事的方式有什么区别?

我知道这是一个老帖子,但截至今天,即随着ECMAScript 6的出现,我们可以声明javascript类。

语法如下:

class Person{
  constructor(name){
    this.name = name;
  }
  printName(){
    console.log('Name is '+this.name);
  }
}
var john = new Person('John Doe');
john.printName(); // This prints 'Name is John Doe'

这篇文章的完整指南可以在这篇文章中找到

你从未在实践中看到过class关键字的原因是所有当前的JavaScript实现都是1.x.

JavaScript 2.0被合并到ECMAScript 4中,这是非常不受欢迎的 ,因此从未进入现实世界。

那么要回答你的问题,你如何使用class关键字? 你不能。

你从未在实践中看到它,因为几乎没有任何东西支持JavaScript 2.0。 该草案来自一个规范,该规范在除草案之外的任何事情之前就已经死

摘要

ES6中引入了class关键字。 class关键字只不过是已经存在的原型继承模式的语法糖。 javascript中的类基本上是编写构造函数的另一种方法,可以使用它来使用new关键字创建新对象。

 class Person { constructor(name) { this.name = name; } talk() { console.log('hi'); } } const me = new Person('Willem'); console.log(typeof Person) // logs function, Person class is just another constructor function under the hood console.log(me.__proto__ === Person.prototype) // logs true, classes just use the same prototypal inheritance pattern which is used by constructor functions. // An object created with the new keyword gets a __proto__ property on it which is a reference to the prototype property on a constructor function. 

在上面的示例中,可以在第一个日志中观察到,从class关键字创建的class实际上是引擎盖下的函数。

console.log(typeof Person) // logs 'function'

es6类使用构造函数使用的相同原型继承模式。 这是另一个演示此行为的示例:

 class Dog { constructor (name) { this.name = name; } bark () { console.log('bark') }; } let doggie = new Dog('fluffy'); doggie.bark(); // logs bark Dog.prototype.bark = () => console.log('woof'); // changing the prototype of Dog, doggie refers to this with its __proto__ property. //Therefore doggie bark method has also changed. doggie.bark(); // logs woof 

以上示例中的内容是任何dog实例的bark方法都可以在运行时更改。 这是因为使用Dog类创建的任何对象的bark方法只是指这个函数。

您仍然可以使用原型在JS中构建类!

var foo = function() {
  this.hurrah = "yay!";
  return this;
}

foo.prototype.doit() {
  alert(this.hurrah);
}

如果你有Java或C#背景,这里是如何在JavaScript中定义一个类

var MyClass = function (f, l){//constructor 
    //private members
    var firstName = f,
        lastName = l,
        fullName = function () { //fullName is a private function
            return firstName + " " + lastName;
        };
    return {
        //public members
        getFullName: fullName 
    };
}

var output = document.getElementById('Output'); //<div id="Output"></div>
var myName = new MyClass("First", "Last");
output.innerHTML = myName.getFullName();

只是添加ECMA5的课堂制作方式。

请注意,它没有这种方式的构造函数(但如果你愿意,你可以触发一个init函数)

var Class = {
      el: null,
      socket: null,

      init: function (params) {

        if (!(this.el instanceof HTMLElement)) {
          throw new Error('Chat room has no DOM element to attach on.');
        }

        return this.doStuff();

      },
      doStuff: function (params) {
        return this;
      }
};

var instanceofClass = Object.create(Class, {
  el: {
    value: document.body.querySelector('.what ever')
  },
  someMoreData: {
    value: [0,5,7,3]
  }
}).init();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM