在JavaScript中声明删除方法是否安全 [英] Is it safe to declare a delete method in JavaScript

查看:60
本文介绍了在JavaScript中声明删除方法是否安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  var request = {
delete:function(url){
//一些代码...
}
}
request.delete('http://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('http://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中声明删除方法是否安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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