本文介绍了覆盖Javascript关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道delete是JavaScript中的关键字.因此,我有以下代码(例如):

I know that delete is a keyword in JavaScript. So I have this code (for example):

var user = {
   create : function () {
       // Create a user account
   },

   delete : function () {
       // Delete a user account
   }
};

以上方法有效(除非使用IE的较早版本),所以我的问题是-这是一个好主意.显然,使用user.delete();的呼叫比使用user.delete_one();

The above works (barring older versions of IE), so my question is - is it a good idea. Obviously the call user.delete(); is much clearer to someone utilizing the code than something like user.delete_one();

显然关键字很重要,但是根据情况,使用这种方法是否还可以(当然,我不需要旧版IE支持)(或者可以使用更好的解决方案吗?)?

Obviously keywords are important, but on a case by case basis is it alright (granted I don't need legacy IE support) to use this method, or is there a better solution?

推荐答案

您的代码将按预期工作,因为您没有覆盖JS关键字.如果尝试将关键字声明为变量或函数名,则JS将显示错误SyntaxError: Unexpected token delete.

You code will work as expected, because you are not overwriting JS keyword. If you try to declare a keyword as variable or function name, JS will show error SyntaxError: Unexpected token delete.

您可以选择但不直接覆盖JS关键字的方式也可以.

It 's alright with the way you choose but don't override JS keywords directly.

这篇关于覆盖Javascript关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:49