我对ES6课程有很多疑问.
因为我了解如何使用函数和WebComponent,React等.我没有看到使用它的许多好处.
我想知道使用类的好处是什么.我读到public/private/static将成为ES7的一部分.所以我认为目前没有必要使用它.
此外,类是OOP的概念还是它仍然是一个'javascript对象概念'?这是否意味着我不能使用它来修改它class?或者它只是相同的对象,但有两种不同的方式来声明它.
速度有好处吗?如果你有一个像Big Java app这样的大型应用程序,可能更容易维护/理解?
class
信息:这个问题现在是3个半哟.由于TJ Crowder,答案仍然准确.但是Javascript已经发展了很多!风险自负.
由于ES6类只是JavaScript现有的基于原型的继承 [1]的语法糖,因此(IMO)提升它的定义是有道理的:
var foo = new Foo(1, 2); //this works
function Foo(x, y) {
this.x = x;
this.y = y;
}
Run Code Online (Sandbox Code Playgroud)
但以下方法不起作用:
var foo = new Foo(1, 2); //ReferenceError
class Foo {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不提升ES6课程?
ECMAScript 2015规范提到关键字(或单词?)new.target正好3次 - 在14.2.3中 1次:
通常,Contains不会查看大多数函数表单但是,Contains用于检测ArrowFunction中的new.target,this和super用法.
在14.2.16中两次:
ArrowFunction不为arguments,super,this或new.target定义本地绑定.对ArrowFunction中的参数,super,this或new.target的任何引用 都必须解析为词汇封闭环境中的绑定
MDN提到它,但非常模糊,页面不完整.
巴别塔似乎不支持它.尝试在函数(箭头或其他)中使用new.target时出现语法错误.
它是什么,它应该如何使用?
在ES6类之前,函数可以用作构造函数:
function MyClass(a, b) {
}
Run Code Online (Sandbox Code Playgroud)
然后,以下代码相当于经典实例化(如let thisObj = new MyClass("A", "B")):
let thisObj = Object.create(MyClass.prototype)
// Here we know the `this` object before to call the constructor.
// Then, the constructor is called manually:
MyClass.call(thisObj, "A", "B")
Run Code Online (Sandbox Code Playgroud)
...这种技术是this在调用构造函数之前知道对象的一种方法.但是Function.prototype.call()不适用于ES6类构造函数.
有了ES6,我们有Reflect.construct():
let thisObj = Reflect.construct(MyClass, "A", "B");
Run Code Online (Sandbox Code Playgroud)
但它没有提供在this创建对象后调用构造函数的方法.
是否仍然可以使用ES6课程?
我需要将此功能从ES5保留到ES6以获得框架.该框架负责实例化组件(这是ES6类).组件可以从其构造函数创建子组件(在组件树中,此处没有继承).然后,子组件可以查询框架以从其自己的构造函数中获取其父级.在这种情况下,我们有技术限制,因为框架仍然没有父组件构造函数的返回值.这是与ES5(转化为)相比的回归.
是否可以通过更改"this"上下文(call,apply或其他)来在另一个实例上使用es6构造函数指令?这可以使用es5"类".这是我的意思的一个小例子:
function ES5() {
this.foo = 'foo';
}
class ES6 {
constructor() {
this.bar = 'bar';
}
}
var a = new ES6();
ES5.call(a);
console.log(a.foo + a.bar); //foobar
var b = new ES5();
//Reflect.construct(ES6); ??
ES6.call(b); //TypeError: Class constructor ES6 cannot be invoked without 'new'
console.log(b.foo + b.bar); //how to get foobar here too?Run Code Online (Sandbox Code Playgroud)
编辑: 我的问题与新关键字无关.我正在寻找的答案是如何使用另一个"this"上下文(带或不带new关键字)运行es6构造函数中的指令.
使用 # 语法,我们现在可以在 ES6 类中创建私有属性,如下所示:
class Person {
#name;
constructor(name) {
this.#name = name;
}
getName() { return this.#name; }
}
let ron = new Person('ron')
ron.#name // undefined
ron.getName(); // ron
Run Code Online (Sandbox Code Playgroud)
以前,在 ES5 中,私有属性很难通过以下方式“伪造”:
function Person(name) {
var name = name;
this.getName = function() {
return name;
}
}
(new Person('ron')).name // undefined
(new Person('ron')).getName() // ron
Run Code Online (Sandbox Code Playgroud)
上面的版本使用了“var”不属于 Person 实例的“this”这一事实。因此,使用“闭包”getName 的力量可以访问“名称”。然而,这样做的问题是 this.getName() 不是原型链的一部分,因此为了将 getName 添加到原型中,我们必须这样做:
Person.prototype.getName = function() { return this.getName(); }
Run Code Online (Sandbox Code Playgroud)
这很令人困惑,而且味道很难闻。
另一种选择是:(使用 ES6 …
所以我打算建立一个小型图书馆,但这个问题有多种应用。
我想知道使用构造函数和类创建对象之间的区别。例如,这段代码...
function Thing (name) {
this.name = name;
this.doSomething = function (){};
alert("A new thing was created.");
}
var x = new Thing();
Run Code Online (Sandbox Code Playgroud)
...以及这段代码...
class Thing {
constructor(name) {
this.name = name;
alert("A new thing was created.");
}
doSomething() {}
}
var x = new Thing();
Run Code Online (Sandbox Code Playgroud)
...产生相同的结果,但以不同的方式。
但是,我更熟悉构造函数,但我需要使用 getter 和 setter 创建对象。尽管MDN将类定义为“语法糖”,但我不知道是否可以使用构造函数定义 getter 和 setter。
还有,女巫的性能是最好的?
注意:我不是指使用Thing.prototype. 我想知道构造函数和类之间的区别。
new.target所以我对Node 6.x 中添加的布尔值做了一些阅读。这是MDNnew.target上提供的一个简单示例
function Foo() {
if (!new.target) throw "Foo() must be called with new";
console.log("Foo instantiated with new");
}
Foo(); // throws "Foo() must be called with new"
new Foo(); // logs "Foo instantiated with new"
Run Code Online (Sandbox Code Playgroud)
但这读起来很像我目前使用下面的代码
var Foo = function (options) {
if (!(this instanceof Foo)) {
return new Foo(options);
}
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
new.target我的问题是:超过方法实例有什么好处吗?我并不认为这两者更清楚。new.target可能是 scosche 更容易阅读,但只是因为它少了一组括号()。
谁能提供我所缺少的见解?谢谢!
这些可互换的语法是否可以创建JS类?我已经习惯了 class语法,但不完全了解它们之间的区别。
function User(name) {
this.name = name;
}
User.prototype.sayHi = function() {
alert(this.name);
}
let user = new User("John");
user.sayHi();
Run Code Online (Sandbox Code Playgroud)
与
class User {
constructor(name) {
this.name = name;
}
sayHi() {
alert(this.name);
}
}
let user = new User("John");
user.sayHi();
Run Code Online (Sandbox Code Playgroud) 我总是被告知"类只是原型的语法糖".
但在这个例子中,这表明这不是真的.
function SubArray() {}
SubArray.prototype = new Array( );
console.log(Array.isArray(new SubArray())) // false
Run Code Online (Sandbox Code Playgroud)
和类相同的例子.
SubArray = class extends Array{}
console.log(Array.isArray(new SubArray())) // true
Run Code Online (Sandbox Code Playgroud)
Funnily instanceof两者都可以正常工作new SubArray instanceof Array.为什么不在这里Array.isArray返回true原型?
我有3个数组:
阵列1是"名字"约翰,吉姆,杰克,吉尔
阵列2是"年龄"25,30,31,22
阵列3是"性别"男性,男性,男性,女性
我将这些都列在一个带有可排序标题的表中.现在排序工作正常,它按"名称"列进行排序.我怎样才能对"年龄"和"性别"数组进行排序以保持正确.我需要同时对所有3个进行排序.
_nameArray.sort(function(a, b){
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
})
Run Code Online (Sandbox Code Playgroud)
希望它解释得很好.
没有结构不能改变,它的方式必须是3个数组,而不是一个,只是按数组中的字段排序.
我看过很多帖子,说Class只是语法糖,或者问Java ES6中Class是否是语法糖
但是我很难理解语法糖的含义(我确实得到使事情易于阅读或理解的字面意思)。
我的问题是Java类中的语法糖是怎样的?
例如,在这个问题中,es6类仅仅是javascript中原型模式的语法糖吗?
就像这个例子有什么关系
class Thing {
//... classy stuff
}
Run Code Online (Sandbox Code Playgroud)
与
var Thing = function() {
// ... setup stuff
};
Thing.prototype.doStuff = function() {};
Run Code Online (Sandbox Code Playgroud)
上面的类中不应该有某种方法(doStuff)使之相等吗?
javascript ×12
ecmascript-6 ×7
class ×3
constructor ×2
arrays ×1
ecmascript-5 ×1
es6-class ×1
instanceof ×1
jquery ×1
new-operator ×1
prototype ×1
this ×1