Just because I'm tired of seeing people say it can't be done...
-
Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:
// this must be a named function
function datNice(): number {
const self = datNice as any;
const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;// this is the only difference between C/C++ since there's no static keyword
// we gotta do this assignment for the juju
self.meSoStatic = meSoStatic;return meSoStatic;
}console.log(datNice()); // 42
console.log(datNice()); // 42Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around
this
I can only assume. But, named functions still work perfectly.Jeremy Falcon
-
Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:
// this must be a named function
function datNice(): number {
const self = datNice as any;
const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;// this is the only difference between C/C++ since there's no static keyword
// we gotta do this assignment for the juju
self.meSoStatic = meSoStatic;return meSoStatic;
}console.log(datNice()); // 42
console.log(datNice()); // 42Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around
this
I can only assume. But, named functions still work perfectly.Jeremy Falcon
So essentially, you've extended the function with a static variable? If I understand this correctly?
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework -
Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:
// this must be a named function
function datNice(): number {
const self = datNice as any;
const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;// this is the only difference between C/C++ since there's no static keyword
// we gotta do this assignment for the juju
self.meSoStatic = meSoStatic;return meSoStatic;
}console.log(datNice()); // 42
console.log(datNice()); // 42Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around
this
I can only assume. But, named functions still work perfectly.Jeremy Falcon
BTW, when I run your code in the Edge console, I get: VM301:1 Uncaught SyntaxError: Unexpected token ':' I assume on the line:
const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;
Same error in Chrome.Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework -
So essentially, you've extended the function with a static variable? If I understand this correctly?
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity FrameworkYup. In functional languages, functions are first-class citizens. Which is just a fancy way of saying they can be treated like any other data type. They have a `this` and are also self referential.
Jeremy Falcon
-
So essentially, you've extended the function with a static variable? If I understand this correctly?
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity FrameworkOh, about the syntax error thingy... it's TypeScript. There's a cool site, kinda like jsfiddle, if you just wanna quickly play around with example code for TS: [https://www.typescriptlang.org/play\](https://www.typescriptlang.org/play). Or [jsfiddle](https://jsfiddle.net/) works if you select the TS language.
Jeremy Falcon
-
So essentially, you've extended the function with a static variable? If I understand this correctly?
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework -
Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:
// this must be a named function
function datNice(): number {
const self = datNice as any;
const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;// this is the only difference between C/C++ since there's no static keyword
// we gotta do this assignment for the juju
self.meSoStatic = meSoStatic;return meSoStatic;
}console.log(datNice()); // 42
console.log(datNice()); // 42Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around
this
I can only assume. But, named functions still work perfectly.Jeremy Falcon
It is all frowned upon just because some "clever beaver" on Google stated so, like sheep, lets follow the rest... I agree 300%, all si possible, how did we get to the "clever" parts, because we used to use 'THIS' first and then tried to make it into rocket science. Blows my mind why we would consistently keep on making things difficult, 1+1=2 point, why try to do 0.8+1.2=2, my 5 cents and upvote! :^)
-
It is all frowned upon just because some "clever beaver" on Google stated so, like sheep, lets follow the rest... I agree 300%, all si possible, how did we get to the "clever" parts, because we used to use 'THIS' first and then tried to make it into rocket science. Blows my mind why we would consistently keep on making things difficult, 1+1=2 point, why try to do 0.8+1.2=2, my 5 cents and upvote! :^)
-
Yes, you can have a static variable in a named function in JavaScript/TypeScript. Even peeps on SO say the only way to achieve this is with a class. Nope. Exhibit A:
// this must be a named function
function datNice(): number {
const self = datNice as any;
const meSoStatic = self.meSoStatic ? self.meSoStatic : 42;// this is the only difference between C/C++ since there's no static keyword
// we gotta do this assignment for the juju
self.meSoStatic = meSoStatic;return meSoStatic;
}console.log(datNice()); // 42
console.log(datNice()); // 42Note: You used to able to pull this off with an anonymous function using arguments.callee, but that's since been deprecated, due to misuse or the confusion around
this
I can only assume. But, named functions still work perfectly.Jeremy Falcon
play with me why is this even relevant in code? I see so much cute, clever, whiz kid bull$$hit it makes my eyes water. I won't miss it.
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.
-
play with me why is this even relevant in code? I see so much cute, clever, whiz kid bull$$hit it makes my eyes water. I won't miss it.
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.
Because sometimes you want persistence without cluttering up the global/module namespace. Makes it pretty easy where to track down what in a larger project. Should it be overdone? Nope. But that goes with anything.
Jeremy Falcon