1 overview
The traditional method of JavaScript language is to define and generate new objects by using constructors. Here is an example.
function point (x, y) {
this.x = x;
This.y = y;
}
Point.prototype.toString = function () {return
' (' + this.x + ', ' + this.y + ') ';
var p = new Point (1, 2);The above is very different from the traditional object-oriented language, such as C + + and Java, and it is easy to confuse the new programmer who is le
Objective
For JavaScript, classes are optional (not necessarily) design patterns, and implementing classes in [[Prototype]] languages like JavaScript is lame.
This kind of lame feeling is not just from grammar, although grammar is a very important reason. JS has a lot of grammatical shortcomings: cumbersome and messy. Prototype refers to explicit pseudo polymorphism when attempting to invoke a function of
class in ES6 is equivalent to a constructor in es5, and if no constructor attribute is defined, an empty constructor is added by default;3,es5 , inheritance is a subclass that first creates its own instance object this, and then invokes the method of the parent class through Parent.call (this) or parent.apply (this);es6
In this article I will take my understanding of the new feature class of JavaScript es6. In the spirit of sharing the Internet, I will share my own understanding to everyone.Write a class (constructor) using ESIn ES5, it's common for everyone to write a class (constructor)It
Es6 notes 7 ^_^ class, es6 notes 7 class
ES6 provides a method closer to the traditional language, introducing the concept of Class as an object template. You can use the class keyword
has become so concise compared to the ES5 version. Cool, not cool?The deconstruction of parameter matchingEqual to (but more concise):Depth matchingEqual to (but more concise):This is also known as the deconstruction of objects.As you can see, deconstruction is very practical and facilitates good coding style.Best Practices:
Use an array to deconstruct to get the element or exchange value. It can avoid creating temporary references.
Instead of using an array to deconstruct multiple
The traditional way of JavaScript language is to define and generate new objects through constructors. Here is an example. (Can be Factory mode, constructor mode, combination of the advantages and disadvantages of the model can be found on the Internet)
function point (x, y) {
This.x=x;
This.y = y;
}
Point.prototype.toString = function () {
Return ' (' +this.x+ ', ' +this.y+ ') ';
}
var p= new Point;
Define the way classes are used in
ECMAScript 6 (hereinafter referred to as ES6) is the next generation of JavaScript language standards. Since the current version of ES6 was released in 2015, it is also called ECMAScript 2015.In other words, ES6 is ES2015.While not all browsers are compatible with ES6 all fe
ES6 new features (Class) and inheritance (Extends) Related Concepts and Usage Analysis, es6extends
This article describes the concepts and usage of new features such as ES6 and Extends. We will share this with you for your reference. The details are as follows:
I. Class)
1. Basic syntax
The traditional method of
ES6 (ECMAScript 6) is the upcoming new version of the JavaScript language standard, code-named Harmony (meaning of harmony, obviously did not keep pace with our country, we have entered the Chinese Dream version). The last standard was enacted in 2009 ES5. The ES6 is currently in the process of standardization and is expected to release the officially finalized v
pointer to point (constructor), and the instance P contains an internal pointer to the prototype object (prop). So the whole inheritance is realized through the prototype chain. The details are visible in my article: Prototype and constructor in JavaScript
Class
ES6 provides a closer approach to the traditional language, introducing the concept of
ECMAScript6 is getting closer and closer to us. As the most important dialect of ECMAScript6, Javascript is about to usher in a major syntax change. InfoQ has set up the "ES6 in depth" column, let's take a look at what new content ES6 will bring us. ECMAScript 6 is getting closer and closer to us. As its most important dialect,
)
Small Glimpse ES6Before testing the ES6 code, we can look at the Io.js support for ES6: https://iojs.org/cn/es6.html.Next, start our Es6-class tour:1. Class BasicsYou should know that in most object-oriented languages
quickly. But after ECMASCript 3 came out of 1999, there were no changes made to the official for a standard. Instead various browser vendors made their own custom extensions to the language, and Web developers were left to try and Support multiple APIs. Even after ECMAScript 5 is published in 2009, it took several years for wide browser support of the new spec, and most de Velopers continued to write code in ECMAScript 3 style, without necessarily being the aware.
Around things started to cha
loading. In essence, the loader handles various files for us. We will use a special babel-loader to translate our ES6 code into ES5 code.
But the coolest thing is that the loader can handle any file. So, if we want to load the style--we just need to add style-loader. Finally, I decided to use less, which is a little more complicated--I'll create the following load chain:
First, use Less-loader to process the *.less file, convert it to CSS, and then u
may be more convenient and faster. Why use ES5,ES6,TypeScript to demo ? Let's introduce these three concepts:ES5: Full Name ECMAScript5, which can be understood as our regular JavaScript(in fact JavaScript also contains the DOM and the BOM ), which is currently supported by most browsers JavaScript syntax characteris
Defining Classes
The ES6 class is not a brand new thing: They mainly provide more convenient syntax for creating old-fashioned constructors, and JavaScript classes are not like classes in other object-oriented language C++,java, where the class is just a syntactic candy, and is actually based on a prototype chain.
, methods that do not want to be exposed can be defined as private methods, but ES6 does not provide private methods and can only be simulated in some ways. However, the Event Callback Function is special, because the event is not only defined, but also removed, which brings extra trouble. But there is still a solution:
Use the Symbol variable to define
Const _ onMouseMove = Symbol ("_ onMouseMove"); class
( This. name);}functionSubtype (name,age) {//Inheritance PropertiesSupertype.call ( This, name);//Call Supertype The second time you create an instance This. Age =Age ;} Inherit (subtype,supertype);//implementing parasitic combined inheritance
Let's take a look at the core function that implements parasitic combined inheritance. The F function is actually generic, and we don't need to declare it every time we enter the inherit function. So we can write in the form of closures:
varInherit =
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.