如何在 JavaScript 中定义一些东西
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16426295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define something in JavaScript
提问by Jessica Lingmn
I know how to define something in C++. For example:
我知道如何在 C++ 中定义一些东西。例如:
#define ce cout<<"\n";
now if we use ceany where of the code it will output cout<<"\n";
I am trying with this JavaSvript:
现在,如果我们使用ce它将输出的代码的任何位置,cout<<"\n";
我正在尝试使用此 JavaSvript:
#define go document.write
So, if i want to show/output something, i wish to write
所以,如果我想显示/输出一些东西,我想写
go('Great!');
now, this will output Great!in the browser. But this definemethod not working in JavaScript
现在,这将Great!在浏览器中输出。但是这种define方法在 JavaScript 中不起作用
回答by Daff
You can't, at least not the way C/C++ does it. You can however assign a function to any variable:
你不能,至少不能像 C/C++ 那样。但是,您可以将函数分配给任何变量:
var go = document.write;
As pointed out this won't work for document.writebecause of the scope change. One solution to get an actual function with the correct scope would be:
正如所指出的,document.write由于范围的变化,这将不起作用。获得具有正确范围的实际功能的一种解决方案是:
var go = document.write.bind(document);
// Now call
go('Great!');
Node that .bindwon't work in IE8 and lower.
.bind无法在 IE8 及更低版本中工作的节点。
回答by Ian
Another option is with Object.defineProperty, which allows you to set something that isn't writable, configurable, or enumerable...specifically on the windowobject (so there is global access). Try this:
另一个选项是 with Object.defineProperty,它允许您设置不可写、可配置或不可枚举的内容……特别是在window对象上(因此有全局访问权限)。试试这个:
Object.defineProperty(window, "go", {
enumerable: false,
configurable: false,
writable: false,
value: function (msg) {
console.log(msg);
}
});
DEMO:http://jsfiddle.net/xm3mT/
演示:http : //jsfiddle.net/xm3mT/
Instead of setting the valueas an anonymous function, you can use bind, like:
value您可以使用bind,而不是将 设置为匿名函数,例如:
value: console.log.bind(console)
Although it would be just as fine to use my first example.
虽然使用我的第一个例子也一样好。
I'm not exactly sure of your use of document.write, but you normally don't want to use that...of course, it's up to you. Nonetheless, I used console.loginstead just for an example.
我不确定您对 的使用document.write,但您通常不想使用它...当然,这取决于您。尽管如此,我使用它console.log只是作为一个例子。
References:
参考:
Object.definePropertydocs - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/definePropertyObject.definePropertybrowser compatibility - http://kangax.github.io/es5-compat-table/#Object.definePropertyFunction.prototype.binddocs - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bindFunction.prototype.bindbrowser compatibility - http://kangax.github.io/es5-compat-table/#Function.prototype.bind
Object.defineProperty文档 - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/definePropertyObject.defineProperty浏览器兼容性 - http://kangax.github.io/es5-compat-table/#Object.definePropertyFunction.prototype.bind文档 - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bindFunction.prototype.bind浏览器兼容性 - http://kangax.github.io/es5-compat-table/#Function.prototype.bind
回答by JimmyRare
You define a variable using the "var" keyword.
您可以使用“var”关键字定义变量。
var name = "Jessica";
var name = "Jessica";
You can assign a function as well:var myFunction = function() {
// do something
};
你也可以分配一个函数:var myFunction = function() {
// do something
};
Javascript is loose typed, so you don't specify what type of variable it is.
Javascript 是松散类型的,因此您无需指定它是什么类型的变量。
回答by user428517
JavaScript is not a compiled language, so there's no equivalent to C++'s define. You'll have to use variables. But you can assign functions to variables (as functions in JavaScript are objects) to get essentially the same result.
JavaScript 不是编译语言,因此没有等同于 C++ 的define. 你将不得不使用变量。但是您可以将函数分配给变量(因为 JavaScript 中的函数是对象)以获得基本相同的结果。

