87,993
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
(function () {
var customService = function () {
};
customService.prototype = {
open: function () {
contents: this._getHtml(),
},
close: function () {
},
_getHtml: function () {
var html = [];
Array.prototype.push.call(html,
'<div class=\"content\">',
'<div>1、</div>',
'<div>2、<\/div>',
'<div>3、<\/div>',
'<div>4、<\/div>',
'<\/div>'
);
return html.join('');
}
};
window.CustomService = new customService();
})();
</script>
<script type="text/javascript">
/*这个是匿名函数,即立即执行函数,相当于声明一个函数让他立即执行,具体如1楼大神问题三描述那样。这里用匿名函数包裹下面执行的代码的用意是创建一个执行环境,防止像customService 这样的变量成为全局变量。*/
(function () {
var customService = function () {
};
customService.prototype = {
open: function () {
contents: this._getHtml(),
},
close: function () {
},
_getHtml: function () {
var html = [];
/*call方法的用意是指定函数执行的上下文,方法的第一个参数即新的函数上下文,也就是说我可以用html去调用push方法,第二个参数是传入push方法的参数*/
Array.prototype.push.call(html,
'<div class=\"content\">',
'<div>1、</div>',
'<div>2、<\/div>',
'<div>3、<\/div>',
'<div>4、<\/div>',
'<\/div>'
);
return html.join('');
}
};
/*这里相当于给customService类创建一个namespace,因为window对象可以省略,以后你在使用customService类时可以这样CustomService.customService*/
window.CustomService = new customService();
})();
</script>