JavaScript函数语法说明:function object.myFunction(){..} [英] JavaScript Function Syntax Explanation: function object.myFunction(){..}

查看:87
本文介绍了JavaScript函数语法说明:function object.myFunction(){..}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为自己能够合理地使用JavaScript并熟悉实现相同功能的许多不同方法。但今天我遇到了一些我以前没见过的函数语法:

I would consider myself to be reasonably competent with JavaScript and familiar with many of the different ways of achieving the same thing. But today I came across some function syntax which I hadn't seen before:

function document.body.onload()
{
    alert('loaded');   
}

如果我要编写这样的代码,我会这样做:

If I were to write such code I would have done it like this:

document.body.onload = function()
{
    alert('loaded');   
}

忽略这不是处理<$ c的最佳方法这一事实$ c> onload 事件,这实际上是有效的JavaScript吗?它似乎导致FireFox(和JSLint)中的语法错误,所以我猜它只是Internet Explorer的语法?如果它只是IE,那么我想删除它,但我担心它可能会有一些古怪的副作用。

Ignoring the fact that this is not the best way to handle the onload event, is this actually valid JavaScript? It appears to causes syntax errors in FireFox (and JSLint), so I am guessing that it is Internet Explorer only syntax? If it is IE only then I would like to remove it but I am concerned that it might have some quirky side effect.

推荐答案

function document.body.onload 是非标准的JavaScript语法。使用第二种格式,并解雇任何人写过原始代码。

function document.body.onload is non-standard JavaScript syntax. Use the second format, and fire whomever wrote the original code.

document.body.onload = function () {
    ...code...
}; //this is easy to miss

这篇关于JavaScript函数语法说明:function object.myFunction(){..}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆