您好,欢迎访问一九零五行业门户网

javascript 写类方式之九_js面向对象

9、yui的写类方式
这里引入的是yui 2.7.0版,只需引入yahoo.js。yui引入了命名空间,类似于java的包。以下yahoo的工具函数包
yahoo.namespace yahoo.lang yahoo.lang.hasownproperty yahoo.lang.extend yahoo.lang.augment yahoo.log yahoo_config and yahoo.env yui module names 写类方式:
复制代码 代码如下:
//定义包名
yahoo.namespace(test);
//定义类
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;}
//创建一个对象
var p = new yahoo.test.person(jack);
console.log(p.getname());//jack
p.setname('tom');
console.log(p.getname());//tom
//测试instanceof及p.constructor是否正确指向了yahoo.test.person
console.log(p instanceof yahoo.test.person);
console.log(p.constructor == yahoo.test.person);
可以看出除了多了包名,与第三种写类方式 并无区别。
其它类似信息

推荐信息