[英]How do you represent a class as a function in JavaScript
Per Classes in MDN MDN 中的每个类
Classes are in fact "special functions"类实际上是“特殊功能”
How would something like怎么会喜欢
class Foo {
constructor(x) {
this.x = x;
}
foo() {}
}
be translated to just function
(ignoring the "hoisting" feature)被翻译成只是function
(忽略“提升”功能)
// declare the "class" function Foo (x) { this.x = x; } // declare the instance method Foo.prototype.foo = function () { return 'i am foo'; } // create an instance const f = new Foo(42); // invoke its methods console.log(f.foo()); // 'i am foo' // access its properties console.log(fx); // 42
Non hoisted , (as much as constructor functions based on class syntax are not hoisted ,) due to an immediately invoked function expression (IIFE) which serves as module pattern ...非悬挂,(多达基于类的语法构造函数不升起,)由于立即被调用函数表达式(IIFE)用作模块图案...
const Foo = (function () { // class Foo {
//
function Foo(x) { // constructor(x) {
this.x = x; // this.x = x;
} // }
function foo() { // foo() {
} //
Foo.prototype.foo = foo; // }
//
return Foo; //
//
}()); // }
Well really everything is objects - a class is simply a blueprint for an object that has some methods and some values.好吧,真的一切都是对象——一个类只是一个对象的蓝图,它有一些方法和一些值。 It's all objects with features that can either store values or transform values.它是具有可以存储值或转换值的功能的所有对象。
A class is something - a function does something.一个类就是一个东西——一个函数做一些事情。 Calling it a special function is if anything unnecessary at best and confusing at worst.称其为特殊功能是最好的不必要的,最坏的情况是令人困惑。
Basically - don't worry about it.基本上 - 不要担心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.