Home >Web Front-end >JS Tutorial >Detailed explanation of examples of Class objects for getting started with ECMAScript6
This article mainly introduces the detailed introduction to ECMAScript6 introduction-Class object. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look.
Object-oriented languages have a sign, that is, they all have the concept of classes, through which any number of objects with the same properties and methods can be created.
There is no concept of class in ECMAScript5, so its objects are different from those in class-based languages.
The traditional way of generating objects in Javascript is through constructors
function Person(name, age){ this.name = name; this.age = age; this.sayHello = function(){ return "Hello "+ this.name; } } var person = new Person("dahan",18); person.sayHello(); //Hello dahan
The above method is the same as the form of declaring methods in Javascript, so the distinction between objects and methods is not obvious. It's easy to get confused.
ES6 introduces the concept of Class
(class). When we create objects through ES6 syntax, we can use the keyword class
like Java syntax. Used to define classes. Of course, the functions of this syntax can also be realized through ES5. It just makes the definition of classes clearer and easier to understand.
//类的定义 class Person { //ES6中新型构造器 constructor(name) { this.name = name; } //实例方法 sayName() { console.log("我的名字叫"+ this.name); } } //类的继承 class Programmer extends Person { constructor(name) { //直接调用父类构造器进行初始化 super(name); } program() { cosnole.log("这是我的地盘"); } } //运行测试 var person = new Person('lingxiao'); var coder = new Programmer('coder'); person.sayName(); //我的名字叫lingxiao coder.sayName(); //我的名字叫coder coder.program(); //这是我的地盘
Let’s pay attention to the syntax that appears in the above code.
constructor
constructor
is the default method of the class, just like the main method in Java, every class must have constructor
method.
When instantiating an object through new
, the constructor
method will be automatically called, and the value returned is the value returned by constructor
. constructor
By default, the instance object of the current class is returned (this)
, but we can also specify another object. Of course, this will result in the instantiated object not being of the current class. Example.
class Person { constructor(){ var ob = new Object(); return Ob; } sayHello(){ return "Hello World" } } var person = new Person(); person.sayHello(); //Uncaught TypeError: person.sayHello is not a function
When we instantiate an object, ES6 stipulates that I use the new keyword. If it is called directly, it will be called as a function.
class Person { constructor(name){ this.name = name; } }; var person = Person("dahan"); //Uncaught TypeError: Class constructor Person4 cannot be invoked without 'new'
this
In the first code, we saw this. This points to the instance itself in the class, but if we are in the method of the class If this is used, an error will occur when this method is called alone.
class Person{ constructor(name){ this.name = name; } sayHello() { return "Hello "+this.name } } var person = new Person("dahan"); var sayHello = person.sayHello; sayHello(); //Uncaught TypeError: Cannot read property 'name' of undefined
For this we can simply bind it in the constructor this
class Person{ constructor(name){ this.name = name; this.sayHello = this.sayHello.call(this); } sayHello() { return "Hello "+this.name } }
Inherit extend
We want If you want to extend some attributes on a class without modifying the original class, you use inheritance.
//类的继承 class Programmer extends Person { constructor(name,age) { this.age = age;//报错 //直接调用父类构造器进行初始化 super(name); } program() { cosnole.log("这是我的地盘"); } }
When using inheritance, you need to use the super
keyword to call the parent class, super(name)
just call the parent class’s constructor
method.
In addition, when we use inheritance, the super
keyword also helps us change the direction of this
, so we must first call super
method before using this
. ES6 requires that the constructor of a subclass must execute the super
function once, otherwise an error will be reported.
Finally
class
The appearance of the keyword also makes Javascript look more like an object-oriented language. I hope Javascript will get better and better. Easy to use.
The above is the detailed content of Detailed explanation of examples of Class objects for getting started with ECMAScript6. For more information, please follow other related articles on the PHP Chinese website!