在JavaScript中声明删除方法是否安全

编程入门 行业动态 更新时间:2024-10-25 01:23:17
本文介绍了在JavaScript中声明删除方法是否安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以问题很简单,如果在JavaScript中声明名为delete的方法是安全的(明智的)。示例:

var request = { delete:function(url){ //一些代码... } } request.delete('page.dev/users/1');

我在Firefox中对此进行了测试并且功能正常,但只是想知道它是否会导致某些问题其它浏览器;或者一般来说,如果这是一个好习惯。

解决方案

如果你使用这个['style'] ,但是使用 this.style ,你应该避免保留字,包括删除。根据规范,使用保留字作为属性名称实际上是可以的,但是无论如何我都会避免它,因为有一些不好的实现(对于使用保留字而言,无论上下文如何,它通常都会有一种糟糕的感觉)。 p>

So the question is simply, if it's safe (wise) to declare method called "delete" in JavaScript. Example:

var request = { delete : function (url) { // Some code... } } request.delete('page.dev/users/1');

I've tested this in Firefox and it's functional, but just wondering if it could cause problems in some other browsers; or in general if is it a good practice.

解决方案

You'll never have an issue if you use this['style'], but with this.style, you should avoid reserved words, which does includes delete. According to the spec, it's actually OK to use reserved words as property names, but I would avoid it anyway because there are some bad implementations around (also it just has a generally bad feel to use reserved words, whatever the context).

更多推荐

在JavaScript中声明删除方法是否安全

本文发布于:2023-10-04 13:41:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声明   方法   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!