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:
//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.