JS一个对象封装多个函数
var aa=function(){
return{
a:function(url){
alert(url);
},
b:function(){
alert(bbbb);
}
}
}();
调用:
aa.a("url");
aa.b();
Js构造对象-添加方法的三种方式
方法一:
function person(name,qq){
this.name = name;
this.qq = qq;
}
person.prototype.showname = function(){
//这里showname即使方法的引用也是方法名,有点奇怪
alert("我的名字:"+this.name);
}
var p1 = new person("张三",11111);
var p2 = new person("李四",22222);
p1.showname();
方法二:
person.prototype = {
showname: function(){
alert('我的名字'+this.name)
}
}
方法三:
person.prototype = function(){
showname = function(){alert('我的名字:'+this.name)}
return { showname: showname}
}()
欢迎分享本文,转载请保留出处:前端ABC » JS一个对象封装多个函数
前端ABC