所以,叫我懒惰,但我正在尝试向 Javascript 的警报功能添加一些语法糖。这是代码:
<script src='jquery.js'></script>
<script>
window.nativeAlert = window.alert
window.alert = function()
{
window.nativeAlert(Array.prototype.slice.call(arguments).join(", ")+" window")
return this
}
Array.prototype.alert = function()
{
window.nativeAlert(this.toString()+" array")
return this
}
Object.prototype.alert = function()
{
window.nativeAlert(this.toString()+" object")
return this
}
$(function()
{
var features = $.getJSON("features.json")
features.alert()
})
</script>
这似乎有效,但发射了两次。知道为什么会这样吗?我的直觉说它与重载有关,但它对数组和 [window.]alert() 工作得很好(触发一次)。
PS 我知道 JSON 还不能正确显示,以后再说。