如何在Javascript中创建静态变量?
当前回答
可以在声明静态变量后重新分配函数
function IHaveBeenCalled() {
console.log("YOU SHOULD ONLY SEE THIS ONCE");
return "Hello World: "
}
function testableFunction(...args) {
testableFunction=inner //reassign the function
const prepend=IHaveBeenCalled()
return inner(...args) //pass all arguments the 1st time
function inner(num) {
console.log(prepend + num);
}
}
testableFunction(2) // Hello World: 2
testableFunction(5) // Hello World: 5
这使用。。。args比较慢,有没有办法第一次使用父函数的作用域而不是传递所有参数?
我的用例:
function copyToClipboard(...args) {
copyToClipboard = inner //reassign the function
const child_process = require('child_process')
return inner(...args) //pass all arguments the 1st time
function inner(content_for_the_clipboard) {
child_process.spawn('clip').stdin.end(content_for_the_clipboard)
}
}
如果要在作用域之外使用child_process,可以将其分配给copyToCclipboard的属性
function copyToClipboard(...args) {
copyToClipboard = inner //reassign the function
copyToClipboard.child_process = require('child_process')
return inner(...args) //pass all arguments the 1st time
function inner(content_for_the_clipboard) {
copyToClipboard.child_process.spawn('clip').stdin.end(content_for_the_clipboard)
}
}
其他回答
在JavaScript中,默认情况下变量是静态的。例子:
var x = 0;
function draw() {
alert(x); //
x+=1;
}
setInterval(draw, 1000);
x的值每1000毫秒递增1它将打印1,2,3等
JavaScript中最接近静态变量的是一个全局变量——这只是一个在函数或对象文本范围之外声明的变量:
var thisIsGlobal = 1;
function foo() {
var thisIsNot = 2;
}
您可以做的另一件事是将全局变量存储在对象文本中,如下所示:
var foo = { bar : 1 }
然后像这样访问变量:foo.bar。
以下示例和解释来自Nicholas Zakas的《Web开发人员专业JavaScript第二版》一书。这是我一直在寻找的答案,所以我认为在这里添加它会很有帮助。
(function () {
var name = '';
Person = function (value) {
name = value;
};
Person.prototype.getName = function () {
return name;
};
Person.prototype.setName = function (value) {
name = value;
};
}());
var person1 = new Person('Nate');
console.log(person1.getName()); // Nate
person1.setName('James');
console.log(person1.getName()); // James
person1.name = 'Mark';
console.log(person1.name); // Mark
console.log(person1.getName()); // James
var person2 = new Person('Danielle');
console.log(person1.getName()); // Danielle
console.log(person2.getName()); // Danielle
本例中的Person构造函数可以访问私有变量名,getName()和setName()方法也是如此。使用此模式,name变量将变为静态,并将在所有实例中使用。这意味着在一个实例上调用setName()会影响所有其他实例。调用setName()或创建新的Person实例会将name变量设置为新值。这会导致所有实例返回相同的值。
可以在声明静态变量后重新分配函数
function IHaveBeenCalled() {
console.log("YOU SHOULD ONLY SEE THIS ONCE");
return "Hello World: "
}
function testableFunction(...args) {
testableFunction=inner //reassign the function
const prepend=IHaveBeenCalled()
return inner(...args) //pass all arguments the 1st time
function inner(num) {
console.log(prepend + num);
}
}
testableFunction(2) // Hello World: 2
testableFunction(5) // Hello World: 5
这使用。。。args比较慢,有没有办法第一次使用父函数的作用域而不是传递所有参数?
我的用例:
function copyToClipboard(...args) {
copyToClipboard = inner //reassign the function
const child_process = require('child_process')
return inner(...args) //pass all arguments the 1st time
function inner(content_for_the_clipboard) {
child_process.spawn('clip').stdin.end(content_for_the_clipboard)
}
}
如果要在作用域之外使用child_process,可以将其分配给copyToCclipboard的属性
function copyToClipboard(...args) {
copyToClipboard = inner //reassign the function
copyToClipboard.child_process = require('child_process')
return inner(...args) //pass all arguments the 1st time
function inner(content_for_the_clipboard) {
copyToClipboard.child_process.spawn('clip').stdin.end(content_for_the_clipboard)
}
}
function Person(){
if(Person.count == undefined){
Person.count = 1;
}
else{
Person.count ++;
}
console.log(Person.count);
}
var p1 = new Person();
var p2 = new Person();
var p3 = new Person();