定义类库
(function (window) {
'use strict'
function defineBayside() {
var Bayside = {};
Bayside.alert = function () {
alert('this is a test messsage from the Bayside framework');
}
return Bayside;
}
if (typeof (Bayside) === 'undefined') {
window.Bayside = defineBayside();
}
})(window);
调用类库
Bayside.alert();
另一种列子 定义类库
function $(selector) {
const self = {
element: document.querySelector(selector),
html: function () {
return self.element
},
on: function (event, callback) {
document.addEventListener(event, callback)
},
hide: function () {
self.element.style.display = "none"
},
attr: function (name, value) {
if (value == null) {
return self.element.getAttribute(name)
} else {
self.element.setAttribute(name, value)
}
}
}
return self;
}
调用类库
$('h3').on('click', function () {
alert('clicked');
$('h2').hide();
$('h1').attr('class', 'hello')
})
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有