Home > Web Front-end > JS Tutorial > JavaScript method nine of writing classes_js object-oriented

JavaScript method nine of writing classes_js object-oriented

WBOY
Release: 2016-05-16 18:50:40
Original
1033 people have browsed it

9. YUI class writing method
What is introduced here is YUI version 2.7.0, just introduce yahoo.js. YUI introduces namespaces, similar to java packages. The following yahoo tool function package

Writing class method:
Copy code The code is as follows:

//Define the package name
YAHOO.namespace("test");

//Define the class
YAHOO.test. Person = function(name) {
this.name = name;
}
YAHOO.test.Person.prototype.setName = function(name){ this.name = name;}
YAHOO. test.Person.prototype.getName = function(){ return this.name;}


//Create an object
var p = new YAHOO.test.Person("jack");

console.log(p.getName());//jack
p.setName('tom');
console.log(p.getName());//tom

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

It can be seen that except for the additional package name, it is no different from the third way of writing classes.
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