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

The seventh way to write classes in javascript_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:50:43975browse

, How to write classes in dojo.js
The latest version of dojo is 1.3.1. I remember it was still 0.4 in 2007. There are gradually more documents, and more and more people use Dojo. Dojo also released a core version, which is only 27kb after compression. Dojo uses the dojo.declare method to define a class. The source code of dojo.declare is not posted here. dojo.declare has three parameters,
Parameter 1: class name className
Parameter 2: inherited class superclass
Parameter 3: constructor, method props
To simply define a class, you only need to pass the first One and three parameters. Because we only discuss how to define a class here, not inheritance. The code is as follows:

Copy code The code is as follows:

//Define class name
var className = "Person";
//Define constructor and method
var proto = {
constructor : function(name){this.name=name;},
getName : function(){ return this.name;},
setName : function(name){ this.name = name;}
}

//Define class Person
dojo.declare(className,null, proto);

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

//Test whether instanceof and p.constructor correctly point to Person
console.log( p instanceof Person);//true
console.log(p.constructor === Person);//true
Statement:
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