为什么JScript中的同名变量和函数会发生冲突?

发布于 2024-08-09 00:35:07 字数 383 浏览 2 评论 0原文

我有一个名为 Age 的变量和一个名为 AGE 的函数。忽略我目前在 JScript 中遇到的不区分大小写的问题,即age(dob) 和 AGE(dob) 都可以工作...

为什么 JScript 尝试将age 变量用作函数?我最好的猜测是,jScript 将函数声明视为声明和表达式...

function AGE(birthDate) { 
     return cmd.Age(birthDate); 
};


var age = 32;
var foo = age; // Error on AGE function

我在 C# 应用程序中使用 ScriptControlClass,并使用 JScript 作为简单的嵌入式语言,为我的用户群提供一些简单到中等复杂度的表达式。 ..

I have a variable named age and a function named AGE. Ignore the problem I am having with JScript beign case insensitive at the moment, i.e. age(dob) and AGE(dob) both work...

Why does JScript try to use the age variable as a function? My best guess would be that jScript is treating the function declaration as both declaration and expression...

function AGE(birthDate) { 
     return cmd.Age(birthDate); 
};


var age = 32;
var foo = age; // Error on AGE function

I am using the ScriptControlClass in a C# application and using JScript as a simple embedded language to provide some simple-medium complexity expression to my user base...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风蛊 2024-08-16 00:35:07

在 JScript/javascript 中,函数是第一类对象。因此,函数也是变量,您可以传递、重用、替换等。

这应该可以解释为什么您不能这样做。在出现错误的行中,不清楚您要分配给 foo 的内容:它可能是age,值为 32 的数字变量。但它也可能是age,函数变量。

换句话说,这是完全合法的:

function age(birthDate) { 
   return cmd.Age(birthDate); 
};

var d = new Date();
var foo = age;
foo(d);

如果

function print42() {
    return 42;
}

print42 = function() {
    return 32;
}

print42(); //legal - returns 32

您担心避免冲突,您可以尝试将函数放入对象中:

var $MyProjectName = function()  { 
    var MyFunctionName = function() {
        // do something
    }

}

// later
var $MyProjectInstance = new $MyProjectName();
$MyProjectInstance.MyFunctionName();

注意名称中的“$”。那里没有什么特别的。 '$' 非常适合用作标识符名称的一部分,并且您希望确保您的名称是唯一的。然而,由于 jQuery 等,$“秘密”开始泄露,因此您还需要添加一些额外的东西来保持独特性。

In JScript/javascript, functions are first class objects. So, functions are also variables that you can pass around, reuse, replace, etc.

That should explain why you can't do this. On the line with the error, it's not clear what you're assigning to foo: it could be age, the numeric variable with a value of 32. But it could also be age, the function variable.

Put another way, this is perfectly legal:

function age(birthDate) { 
   return cmd.Age(birthDate); 
};

var d = new Date();
var foo = age;
foo(d);

and so is this:

function print42() {
    return 42;
}

print42 = function() {
    return 32;
}

print42(); //legal - returns 32

If you're worried about avoided conflicts, you could try putting your functions inside an object instead:

var $MyProjectName = function()  { 
    var MyFunctionName = function() {
        // do something
    }

}

// later
var $MyProjectInstance = new $MyProjectName();
$MyProjectInstance.MyFunctionName();

Note the '$' in the names. There's nothing special there. '$' is perfectly fine for use as part of an identifier name, and you want to make sure your name is unique. However, thanks to jQuery and the like the $ "secret" is starting to get out, and so you want to also add a little something extra to keep things unique.

离线来电— 2024-08-16 00:35:07

确保不要为函数和变量指定相同的名称。如果您有一个名为 my_cat 的变量和一个名为 my_cat 的函数,JavaScript 将忘记该函数应该执行的操作或您在 my_cat 变量中存储的值。由于这种奇怪的行为,并且由于函数名称区分大小写,因此对函数命名与对变量命名采用不同的约定是有意义的。

Make sure you don't give a function and a variable the same name. If you have a variable called my_cat and a function called my_cat, JavaScript will forget either what the function's supposed to do or what value you've stored in the my_cat variable. Because of this weird behavior, and because function names are case sensitive, it makes sense to have a different convention for naming functions than for naming variables.

电影里的梦 2024-08-16 00:35:07

这是标准 JavaScript 语法,在此 JavaScript 提示 页面上进行了摘要描述。你应该看一下 JavaScript 参考;很多人推荐 JavaScript: The Good Parts,由 JavaScript 的原作者编写,但你应该这样做买书之前先研究一下。

This is standard JavaScript syntax, described in summary on this JavaScript tips page. You should take a look at a JavaScript reference; many people recommend JavaScript: The Good Parts, written by the original author of JavaScript, but you should do your research before buying a book.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文