我见过代码:
var WAX = function () {
var _arrInputs;
window.addEventListener('waxSetArr', function(evt) {
_arrInputs = evt.detail;
});
return {
getElement: function (i) {
return _arrInputs[i];
}
}
}();
function waxGetElement(i) {
return WAX.getElement(i);
}
我在我的网站上发现了这段奇怪的javascript代码,即使我并不想添加它。我在互联网上搜索了它的作用。我仍然无法真正得到它的作用。这段代码做了什么,有人可以向我解释一下吗?
答案 0 :(得分:1)
添加了一些解释代码本身的注释。无法告诉你它在你所拥有的页面的上下文中实际做了什么,因为没有足够的信息,但你应该能够弄明白。
//Creates a function and assigns it to the variable WAX. This is then
//also called at this point.
var WAX = function () {
var _arrInputs;
//Adds an event listener - when 'waxSetArr' is raised
//this will set the _arrInputs to equal the evt.detail.
//(Which would give the appearance of being an array).
window.addEventListener('waxSetArr', function(evt) {
_arrInputs = evt.detail;
});
//Returns a function, that when called will return the item
//in the array at index i.
return {
getElement: function (i) {
return _arrInputs[i];
}
}
}();
//Calls the function stored in the variable above - returns the element
//at index i (if it exists).
function waxGetElement(i) {
return WAX.getElement(i);
}