In the process of writing and using js programs, we often encounter various errors. For a language that depends on the browser, it is difficult for us to completely control its correct operation at any time. But we still need to make our own efforts to enhance the robustness and security of the js program code we write and reduce the probability of errors as much as possible.
The following are some of my thoughts on enhancing the robustness of js programs that I have summarized in the process of learning js. If you think it is of some value to you, then I have achieved my goal. If you think it is meaningless, Please don't throw bricks, thank you.
(1) It is necessary to determine whether the necessary parameters are passed in correctly.
Code example:
]
(2) Different processing is performed according to the type of parameters passed in:
For example, when we want to obtain an html element object, we need to determine whether the passed in is an id or an element object.
Code example:
If you need to introduce external Js, you need to refresh to execute
]
(3) Set default values for parameters.
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
](4) Check the type of parameters passed in.
For example:
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh it to execute
]
[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]
[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
(6)在解析xml数据之前首先要检查传入xml数据类型(Dom?xml字符串?文件名)及其结构的合法性。 示例代码:
(7)善用try...catch语句,很多时候的错误我们无法屏蔽,使用try...catch可以很容易的将这些错误过滤掉。 比如当我们在使用userData时,很多浏览器不支持,使用try...catch可以简化我们的处理。
欢迎大家多评论,提出更好更多的建议。
arg==undefined 改成 arg === undefined 可能会好些
var arg1=arg1||"default arg1";
这样写略有点问题
arg1是null 0 "" false都不行
三个等号表示类型也相等。JS在比较时不会自动转换类型。