Home > Web Front-end > JS Tutorial > The eighth way to write classes in javascript_js object-oriented

The eighth way to write classes in javascript_js object-oriented

WBOY
Release: 2016-05-16 18:50:41
Original
1098 people have browsed it
8. How to write classes in Ext.js
Ext core3.0 is used here. Ext.extend is used in Ext to define a class (of course it is more used to extend a class). Ext Various controls in the entire framework, such as Panel, MessageBox, etc., are extended using the Ext.extend method. Here we only use it to define the simplest class.
Looking at the code of Ext.extend, we can see that it still uses constructors and prototypes to assemble a class.
You only need to pass two parameters here. The first parameter is the root class Object, and the second is the prototype.
Copy code The code is as follows:

/**
* Person类
* @param {Object} name
*/
var Person = Ext.extend(Object,{
constructor : function(name) {this.name = name;},
setName : function(name) {this.name = name;},
getName : function() {return this.name;}
});



//Create an object
var p = new Person("Lily");
console.log(p.getName());//Lily
p.setName("Andy");
console.log(p.getName());//Andy

/ /Test whether instanceof and p.constructor correctly point to Person
console.log(p instanceof Person);//true
console.log(p.constructor == Person);//true

What is special is that if you simply define a class, then the first parameter can always be passed as Object.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template