gpt4 book ai didi

javascript - 适用于 MooTools 框架的 Google Closure Compiler 外部文件

转载 作者:行者123 更新时间:2023-11-29 22:35:03 25 4
gpt4 key购买 nike

Google Closure Compiler 在高级优化模式下使用一个文件来定义 externs,这将防止引用外部库中符号的变量、属性和函数名称被缩小,从而破坏代码。我的目标是编译使用 MooTools 框架(版本 1.2 和 1.3)的代码,但我无法找到一个“官方”外部文件,当我的代码被提供给闭包编译器时,它可以防止对 MooTools 函数的引用被混淆。我提出了以下相当基本的自定义外部文件,它成功地处理了我的文件,但是否有更好的选择(例如从 MooTools 源自动生成它)?

//
// Number
//

/**
* @param {number} min
* @param {number} max
* @return {number}
* @nosideeffects
*/
Number.prototype.limit = function (min, max) {}
/**
* @param {number=} precision
* @return {number}
* @nosideeffects
*/
Number.prototype.round = function (precision) {}
Number.prototype.times = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.toFloat = function () {}
/**
* @param {number=} base
* @return {number}
* @nosideeffects
*/
Number.prototype.toInt = function (base) {}

//
// Number.Math
//

/**
* @return {number}
* @nosideeffects
*/
Number.prototype.abs = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.acos = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.asin = function () {}
/**
* @param {number} x
* @return {number}
* @nosideeffects
*/
Number.prototype.atan2 = function (x) {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.ceil = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.cos = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.exp = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.floor = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.log = function () {}
/**
* @param {...number} x
* @return {number}
* @nosideeffects
*/
Number.prototype.max = function (x) {}
/**
* @param {...number} x
* @return {number}
* @nosideeffects
*/
Number.prototype.min = function (x) {}
/**
* @param {number} x
* @return {number}
* @nosideeffects
*/
Number.prototype.pow = function (x) {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.sin = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.sqrt = function () {}
/**
* @return {number}
* @nosideeffects
*/
Number.prototype.tan = function () {}

//
// String
//

/**
* @return {boolean}
* @nosideeffects
*/
String.prototype.test = function () {}
/**
* @return {boolean}
* @nosideeffects
*/
String.prototype.contains = function () {}
/**
* @return {string}
* @nosideeffects
* @suppress {duplicate}
*/
String.prototype.trim = function () {}
/**
* @return {string}
* @nosideeffects
*/
String.prototype.clean = function () {}
/**
* @return {string}
* @nosideeffects
*/
String.prototype.camelCase = function () {}
/**
* @return {string}
* @nosideeffects
*/
String.prototype.hyphenate = function () {}
/**
* @return {string}
* @nosideeffects
*/
String.prototype.capitalize = function () {}
/**
* @return {string}
* @nosideeffects
*/
String.prototype.escapeRegExp = function () {}
/**
* @return {number}
* @nosideeffects
*/
String.prototype.toInt = function () {}
/**
* @return {number}
* @nosideeffects
*/
String.prototype.toFloat = function () {}
/**
* @return {(string|Array.<number>)}
* @nosideeffects
*/
String.prototype.hexToRgb = function () {}
/**
* @return {(string|Array.<string>)}
* @nosideeffects
*/
String.prototype.rgbToHex = function () {}
/**
* @return {string}
* @nosideeffects
*/
String.prototype.substitute = function () {}
/**
* @return {string}
* @nosideeffects
*/
String.prototype.stripScripts = function () {}

//
// Array
//

/**
* @param {function(*, number=, Array=) : undefined} fn
* @param {!Object=} bind
*/
Array.prototype.each = function (fn, bind) {}
Array.prototype.invoke = function () {}
/**
* @param {function(*, number=, Array=) : boolean} fn
* @param {!Object=} bind
* @return {boolean}
* @suppress {duplicate}
*/
Array.prototype.every = function (fn, bind) {}
/**
* @param {function(*, number=, Array=) : boolean} fn
* @param {!Object=} bind
* @return {Array}
* @suppress {duplicate}
*/
Array.prototype.filter = function (fn, bind) {}
/**
* @return Array
*/
Array.prototype.clean = function () {}
/**
* @param {*} item
* @param {number} from
* @return number
* @suppress {duplicate}
*/
Array.prototype.indexOf = function (item, from) {}
/**
* @param {function(*, number=, Array=) : *} fn
* @param {!Object=} bind
* @return {Array}
* @suppress {duplicate}
*/
Array.prototype.map = function (fn, bind) {}
/**
* @param {function(*, number=, Array=) : boolean} fn
* @param {!Object=} bind
* @return {boolean}
* @suppress {duplicate}
*/
Array.prototype.some = function (fn, bind) {}
/**
* @param {Array} obj
* @return {Object}
*/
Array.prototype.associate = function (obj) {}
/**
* @param {Object} obj
* @return {Object}
*/
Array.prototype.link = function (obj) {}
/**
* @param {*} item
* @param {number} from
* @return {boolean}
*/
Array.prototype.contains = function (item, from) {}
/**
* @param {Array} other
* @return {Array}
*/
Array.prototype.append = function (other) {}
/**
* @return {*}
*/
Array.prototype.getLast = function () {}
/**
* @return {*}
*/
Array.prototype.getRandom = function () {}
/**
* @param {*} item
* @return {Array}
*/
Array.prototype.include = function (item) {}
/**
* @param {Array} other
* @return {Array}
*/
Array.prototype.combine = function (other) {}
/**
* @param {*} item
* @return {Array}
*/
Array.prototype.erase = function (item) {}
/**
* @return {Array}
*/
Array.prototype.empty = function () {}
/**
* @return {Array}
*/
Array.prototype.flatten = function () {}
/**
* @return {*}
*/
Array.prototype.pick = function () {}
Array.prototype.hexToRgb = function () {}
Array.prototype.rgbToHex = function () {}

// Class

/**
* @constructor
* @param {(!Object|function())} properties
*/
function Class(properties) { }
/**
* @param {!Object} properties
*/
Class.prototype.implement = function (properties) {}

/**
* @constructor
*/
function Events() { }
/**
* @constructor
*/
function Options() { }

//
// Element
//

/**
* @constructor
* @param {(string|Element)} el
* @param {!Object=} properties
* @suppress {duplicate}
*/
function Element(el, properties) { }
/**
* @param {string} tag
* @return {Element}
*/
Element.prototype.getElement = function (tag) {}
/**
* @param {string} tag
* @return {Elements}
*/
Element.prototype.getElements = function (tag) {}
/**
* @param {string} id
* @return {Element}
*/
Element.prototype.getElementById = function (id) {}
/**
* @param {(string|!Object)} property
* @param {*=} value
* @return {Element}
*/
Element.prototype.set = function (property, value) {}
/**
* @param {string} property
* @return {*}
*/
Element.prototype.get = function (property) {}
/**
* @param {string} property
* @return {*}
*/
Element.prototype.erase = function (property) {}
/**
* @param {(string|Element)} match
* @return {boolean}
*/
Element.prototype.match = function (match) {}
/**
* @param {(string|Element)} el
* @return {boolean}
* @suppress {duplicate}
*/
Element.prototype.contains = function (el) {}
/**
* @param {(string|Element)} el
* @param {string=} where
* @return {Element}
*/
Element.prototype.inject = function (el, where) {}
/**
* @param {(string|Element)} el
* @param {string=} where
* @return {Element}
*/
Element.prototype.grab = function (el, where) {}
/**
* @param {(string|Element|Elements)} el
* @param {...(string|Element|Elements)} var_args
* @return {Element}
*/
Element.prototype.adopt = function (el,var_args) {}
/**
* @param {(string|Element)} el
* @param {string=} where
* @return {Element}
*/
Element.prototype.wraps = function (el, where) {}
/**
* @param {string} text
* @param {string=} where
* @return {Element}
*/
Element.prototype.appendText = function (text, where) {}
/**
* @return {undefined}
*/
Element.prototype.dispose = function () {}
/**
* @param {boolean=} contents
* @param {boolean=} keepid
* @return {Element}
*/
Element.prototype.clone = function (contents, keepid) {}
/**
* @param {(string|Element)} el
* @return {Element}
*/
Element.prototype.replaces = function (el) {}
/**
* @param {string} className
* @return {boolean}
*/
Element.prototype.hasClass = function (className) {}
/**
* @param {string} className
* @return {Element}
*/
Element.prototype.addClass = function (className) {}
/**
* @param {string} className
* @return {Element}
*/
Element.prototype.removeClass = function (className) {}
/**
* @param {string} className
* @param {boolean=} force
* @return {Element}
*/
Element.prototype.toggleClass = function (className, force) {}
/**
* @param {string=} match
* @return {Element}
*/
Element.prototype.getPrevious = function (match) {}
/**
* @param {string=} match
* @return {Elements}
*/
Element.prototype.getAllPrevious = function (match) {}
/**
* @param {string=} match
* @return {Element}
*/
Element.prototype.getNext = function (match) {}
/**
* @param {string=} match
* @return {Elements}
*/
Element.prototype.getAllNext = function (match) {}
/**
* @param {string=} match
* @return {Element}
*/
Element.prototype.getFirst = function (match) {}
/**
* @param {string=} match
* @return {Element}
*/
Element.prototype.getLast = function (match) {}
/**
* @param {string=} match
* @return {Element}
*/
Element.prototype.getParent = function (match) {}
/**
* @param {string=} match
* @return {Elements}
*/
Element.prototype.getParents = function (match) {}
/**
* @param {string=} match
* @return {Elements}
*/
Element.prototype.getSiblings = function (match) {}
/**
* @param {string=} match
* @return {Elements}
*/
Element.prototype.getChildren = function (match) {}
/**
* @return {Element}
*/
Element.prototype.empty = function () {}
Element.prototype.destroy = function () {}
/**
* @return {string}
*/
Element.prototype.toQueryString = function () {}
Element.prototype.getSelected = function () {}
/**
* @param {string} property
* @return {string}
*/
Element.prototype.getProperty = function (property) {}
/**
* @param {Array.<string>} properties
* @return {!Object}
*/
Element.prototype.getProperties = function (properties) {}
/**
* @param {string} property
* @param {*} value
* @return {Element}
*/
Element.prototype.setProperty = function (property, value) {}
/**
* @param {!Object} properties
* @return {Element}
*/
Element.prototype.setProperties = function (properties) {}
/**
* @param {string} property
* @return {Element}
*/
Element.prototype.removeProperty = function (property) {}
/**
* @param {Array.<string>} properties
* @return {Element}
*/
Element.prototype.removeProperties = function (properties) {}
/**
* @param {string} key
* @param {*} value
* @return {Element}
*/
Element.prototype.store = function (key, value) {}
/**
* @param {string} key
* @param {*=} def
* @return {*}
*/
Element.prototype.retrieve = function (key, def) {}
/**
* @param {string} key
* @return {Element}
*/
Element.prototype.eliminate = function (key) {}

/**
* @constructor
* @extends Array
*/
function Elements() { }
/**
* @param {(string|!Object)} property
* @param {*=} value
* @return {Element}
*/
Elements.prototype.set = function (property, value) {}
/**
* @param {string} className
* @return {Element}
*/
Elements.prototype.addClass = function (className) {}
/**
* @param {string} className
* @return {Element}
*/
Elements.prototype.removeClass = function (className) {}
/**
* @param {string} className
* @param {boolean=} force
* @return {Element}
*/
Elements.prototype.toggleClass = function (className, force) {}

/**
* @param {(string|Element)} arg
* @return {Element}
*/
function $(arg) {}
/**
* @param {(string|Array|Element|Elements)} arg
* @param {...Element} var_args
* @return {Elements}
*/
function $$(arg, var_args) {}

//
// Element.Style
//

/**
* @param {string} property
* @return {Element}
*/
Element.prototype.setStyle = function (property, value) {}
/**
* @param {string} property
* @return {string}
*/
Element.prototype.getStyle = function (property) {}
/**
* @param {!Object} styles
* @return {Element}
*/
Element.prototype.setStyles = function (styles) {}
/**
* @param {...string} var_args
*/
Element.prototype.getStyles = function (var_args) {}

//
// Element.Event
//

/**
* @param {string} type
* @param {function()} fn
* @return {Element}
*/
Element.prototype.addEvent = function (type, fn) {}
/**
* @param {string} type
* @param {function()} fn
* @return {Element}
*/
Element.prototype.removeEvent = function (type, fn) {}
/**
* @param {!Object} events
* @return {Element}
*/
Element.prototype.addEvents = function (events) {}
/**
* @param {(string|!Object)=} events
* @return {Element}
*/
Element.prototype.removeEvents = function (events) {}
/**
* @param {string} type
* @param {(Array|Object)=} args
* @param {number=} delay
* @return {Element}
*/
Element.prototype.fireEvent = function (type, args, delay) {}
/**
* @param {Element} from
* @param {string} type
* @return {Element}
*/
Element.prototype.cloneEvents = function (from, type) {}

/**
* @param {string} type
* @param {function()} fn
*/
window.addEvent = function (type, fn) {}
/**
* @param {string} type
* @param {function()} fn
*/
window.removeEvent = function (type, fn) {}
/**
* @param {!Object} events
*/
window.addEvents = function (events) {}
/**
* @param {(string|!Object)=} events
*/
window.removeEvents = function (events) {}
/**
* @param {string} type
* @param {(Array|Object)=} args
* @param {number=} delay
*/
window.fireEvent = function (type, args, delay) {}
/**
* @param {Element} from
* @param {string} type
*/
window.cloneEvents = function (from, type) {}

//
// Element.Dimensions
//

Element.prototype.scrollTo = function () {}
Element.prototype.getSize = function () {}
Element.prototype.getScrollSize = function () {}
Element.prototype.getScroll = function () {}
Element.prototype.getPosition = function () {}
Element.prototype.setPosition = function () {}
Element.prototype.getCoordinates = function () {}
Element.prototype.getOffsetParent = function () {}

window.scrollTo = function () {}
window.getSize = function () {}
window.getScrollSize = function () {}
window.getScroll = function () {}

//
// Fx
//

/**
* @constructor
*/
function Fx() {}
/**
* @constructor
* @param {(string|Element)} el
* @param {!Object=} options
*/
Fx.Tween = function (el, options) { }
Fx.Tween.prototype.set = function () { }
Fx.Tween.prototype.start = function () { }
/**
* @constructor
* @param {(string|Element)} el
* @param {!Object=} options
*/
Fx.Morph = function (el, options) { }
/**
* @param {(string|!Object)} properties
* @return {Fx.Morph}
*/
Fx.Morph.prototype.set = function (properties) { }
/**
* @param {(string|!Object)} properties
* @return {Fx.Morph}
*/
Fx.Morph.prototype.start = function (properties) { }

/**
* @return {Element}
*/
Element.prototype.tween = function () {}
/**
* @return {Element}
*/
Element.prototype.morph = function () {}
/**
* @return {Element}
*/
Element.prototype.fade = function () {}
/**
* @return {Element}
*/
Element.prototype.highlight = function () {}

//
// Fx.Transitions
//

/**
* @constructor
*/
Fx.Transitions = function () {}
Fx.Transitions.linear = function () {}
Fx.Transitions.quad = function () {}
Fx.Transitions.cubic = function () {}
Fx.Transitions.quart = function () {}
Fx.Transitions.quint = function () {}
Fx.Transitions.pow = function () {}
Fx.Transitions.expo = function () {}
Fx.Transitions.circ = function () {}
Fx.Transitions.sine = function () {}
Fx.Transitions.back = function () {}
Fx.Transitions.bounce = function () {}
Fx.Transitions.elastic = function () {}

//
// Request
//

/**
* @constructor
* @param {!Object=} options
*/
function Request(options) { }
Request.prototype.setHeader = function () {}
Request.prototype.getHeader = function () {}
Request.prototype.send = function () {}
Request.prototype.post = function () {}
Request.prototype.get = function () {}
Request.prototype.put = function () {}
Request.prototype.cancel = function () {}
Request.prototype.isRunning = function () {}

Element.prototype.send = function () {}

/**
* @constructor
* @extends Request
* @param {!Object=} options
*/
Request.HTML = function (options) { }
/**
* @constructor
* @extends Request
* @param {!Object=} options
*/
Request.JSON = function (options) { }

//
// JSON
//

/**
* @constructor
*/
function JSON() { }
/**
* @param {Object} obj
* @return {string}
*/
JSON.encode = function (obj) {}
/**
* @param {string} string
* @param {boolean=} secure
*/
JSON.decode = function (string, secure) {}

//
// Swiff
//

/**
* @constructor
* @param {string} path
* @param {!Object} options
*/
function Swiff(path, options) { }

//
// Compat
//

/**
* @param {...*} var_args
* @nosideeffects
*/
function $pick(var_args) { }
/**
* @param {...Object} var_args
* @nosideeffects
*/
function $merge(var_args) { }

最佳答案

全面的扩展文件只是在高级模式下使用 Closure 编译器的第一步。必须编写(或修改)库本身才能使用。

externs 文件只能防止一类问题——属性和函数重命名。但是,如果不仔细清理库代码库,某些代码结构可能会在编译后中断。

例如,死代码删除可以很容易地删除错误代码(或留下应该删除的代码)。意外的别名可以阻止大多数优化的发生。 “危险地使用它”可以在最毫无戒心的地方破坏代码。

因此,即使您的代码可以编译并与您的 externs 文件一起正常工作,也不意味着其他 MooTools 程序也可以正常工作。此外,这并不意味着当您在不知不觉中使用了在编译时中断的功能时,您的程序的下一个修订版不会中断。

关于javascript - 适用于 MooTools 框架的 Google Closure Compiler 外部文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com