|
Subject: Javascript
Marc Clifton pulls pin, rolls grenade into forum...
With just one simple word.
|
|
|
|
|
JavaScript can be pretty neat, but with lots of rough edges.
> Classes are cool, but then there's the "class-like" hack of creating an object with key-value pairs where the key is the function "name" and the value is the function. Is that old school?
That's one old school way to do it, but it's not quite equivalent. Creating an object where the function names are the keys in the key value pairs is equivalent to creating a class with nothing but static methods in C#. Of course, using ES2017+ syntax, you can just create static functions in your JS class.
So something like this:
const AnObject {
aFunction: function() {
console.log("hi");
}
}
ends up being exactly the same as
class AClass {
static aFunction() {
console.log("hi");
}
}
The old school equivalent to a class declaration would look like this:
function MyClass(abc) {
this.abc = abc;
}
MyClass.prototype.printAbc = function() {
console.log(abc);
}
Which ends up doing the same as:
class MyClass {
constructor(abc) {
this.abc = abc;
}
printAbc() {
console.log(abc);
}
}
In either case, the following code will have exactly the same result:
let something = new MyClass();
something.printAbc();
The class syntax is mostly just syntactic sugar; at the end of the day, the JavaScript runtime ends up doing what's shown in the first example, i.e. creating a constructor function and then adding the class's methods to the constructor function's prototype.
Understanding protytypes is pretty useful. Understanding them means you understand how the entire JS class system works. And fortunately, protytypes aren't that big a cesspool if you've got a good teacher. Probably the best resource on the topic (even though it's quite old now) is JavaScript: The Good Parts. It's an easy read, and enjoyable because the author is a bit of a grump and isn't shy about noting the bad parts of JS along with the good ones.
The book predates some of the newer syntax, like let, const, and even class. But if you keep in mind that the class syntax is really just using prototypes under the hood, then almost everything in the book will be very relevant to what you're doing now.
One thing the book won't cover is Promises, along with how the new async/await that simplifies using Promises. Its very, very similar to how the async/await syntax in C# makes it easy to work with Tasks.
|
|
|
|
|
That was a fantastic write-up!
Latest Article - Contextual Data Explorer
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
Next to the book of Douglas Crockford, I would also recommend the videos on youtube "Crockford on Javascript". It is fun to watch and gives a good insight where Javascript comes from.
If on the otherhand you want a compiler, please do evaluate typescript. It is a language which is strongly typed and compiles to Javascript.
Have fun with Javascript.
|
|
|
|
|
I spend 2 years on a project using Typescript (from 0.8.xx to 2.x) along with JavaScript. At the end, on new projects, I went back to pure JS. I felt like the static type in TS is getting in the way and hinder me from what I want to do sometime. It reminded me back when I was coding using Assembler and C for kernel development. Man I feel old.
|
|
|
|
|
What did you drink Mr.Clifton ?
Caveat Emptor.
"Progress doesn't come from early risers – progress is made by lazy men looking for easier ways to do things." Lazarus Long
|
|
|
|
|
|
You may as well just change over to VB at this point, and be done with it. Hippie.
Typeless languages are for people that lack the mental fortitude to work within strongly-typed constraints.
I bet you drink coffee at Starbucks, instead of out of a boot, or a rusty can (like real men).
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 22-Mar-18 14:48pm.
|
|
|
|
|
John Simmons / outlaw programmer wrote: I bet you drink coffee at Starbucks, instead of out of a boot, or a rusty can (like real men).
Even worse, I drink tea, not coffee! Earl Grey at that!
Latest Article - Contextual Data Explorer
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
Marc Clifton wrote: Even worse, I drink tea, not coffee! Earl Grey at that!
BAH!
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Let me buy you a Zima, then! LOL
|
|
|
|
|
LOL.
That's how I feel about Garbage Collected Languages... Or, er, "Managed Code".
|
|
|
|
|
Great topic!
The tools available these days for debugging are awesome compared to around 20 years ago when I started learning/using javascript. Alert() was my only friend! Some of those earliest javascript functions I wrote have survived mostly unchanged.
That said, I was frustrated the other day at the lack of built-in date/time formatting...you want a nice looking date, you have to roll your own function! Yeah, it has it's quirks, but once you understand them, it's amazing what you can do with javascript/css.
It's also amazing to me how many javascript developers don't error-proof their code, apparently being content to let the browser eat the exceptions...except that some clients (don't ask me why) have enabled debugging and get to see all those nasty little undefined and NaN errors, and even get the opportunity to debug it. Yeah, this really confuses the end users, 'cause they're really not sure what to do as this is not covered in the product documentation...so they call, or send a screenshot...and I think to myself:
a: why do they have debugging enabled in the first place
b: the question is quite clear to me...to debug or not to debug (internal voice sarcastically, 'are you a f*%king programmer? Of course you dumb twit, you should click the Cancel button!)'
What's painful is setting up for debugging, then using that browser in the real world. Cancel...Cancel...Cancel...Cancel...∞
"Go forth into the source" - Neal Morse
|
|
|
|
|
my favorite feature is using || to override missing parameters!!!
var timerSetting = paramTimer || userTimer || systemTimer || 500;
the functions as parameters... That was a Throwback to CLIPPER days, where you had code snippets:
{|| code} you could pass as parameters for event handlers, etc. Way cool... First language that I ever saw that in (probably because I took LISP later)... But you cdr guessed that. LOL
I am HOPING that JavaScript replaces VBA as the internal scripting language of ALL programs!
The more I use it, the more I like it. Kinda like JSON. The more you get used to it... the less you want to kill someone...
|
|
|
|
|
Kirk 10389821 wrote: my favorite feature is using || to override missing parameters!!!
Thanks, haven't tried that!
My favorite feature is writing a function that creates a new document on the fly, including functions/handlers of it's own, which in turn, creates functions/handlers for another new doc... but it works! (great way to control prints on tables)
"Go forth into the source" - Neal Morse
|
|
|
|
|
Marc Clifton wrote: this and bind is someone's idea of hell, but once you figure it out, it works.
This perfectly sums up how I feel about this as well.
Marc Clifton wrote: I haven't even touched what looks like the cesspool of prototype . Probably something I should learn.
It's really not that bad
When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.
|
|
|
|
|
I think you might like Typescript and visual studio code
|
|
|
|
|
The feeling I always get with Javascript is that feeling of insecurity... 'I presumed that parameter will be a number and it always will be....won't it?'.
Maybe I like my static typing 'crutch' too much, or maybe it's because I like the idea of domain modelling with types, but things like TypeScript and PureScript appeal to me as Javascript front-ends more than Javascript itself...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
|
Most modern language lack set data type. Remember old Pascal set. For example
if ("A" in ["A","B","C"]) then
.. do something.
I implemented it in Javascript like this.
function SetOf(arr) {
var obj = {};
for (var i = 0; i < arr.length; i++) {
obj[arr[i]] = arr[i];
}
return obj;
}
if ('A' in SetOf(['A','B','C']))
.. do something.
|
|
|
|
|
Sander Rossel wrote: I'll admit that you usually don't need LINQ in your browser and getting arrgh just for remove is a bit overkill
At the moment, I'm not doing any manipulation of arrays and dictionaries other than adding items to them and iterating over them. But that might change...
Latest Article - Contextual Data Explorer
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
I spent about 6 months in developer hell working with JavaScript ("cool--now can you add these 5 things?") about 5 years ago, and was fairly deep into some of these concepts.
Basic premise: Create a web UI to do scheduling for 5,000 workers (construction, lab, etc) that talked to the (very old) scheduling system.
Kink in the process: Needed to account for all sorts of static scheduling rules (start times, end times, allowed ranges, etc) and a variable list of union rules (lots of different unions on site) to match up and validate the work, the pay rates for job type, and such.
My JS coding was definitely old-school style, but prototypes were what saved the day. Date and time handling, rules, etc allowed me to push the common functions further up the chain, and going between 5 different datetime formats certainly made a difference. Same for the "rules" concept--basic rules and functions, and subclasses for specialized parameters.
The whole thing was cancelled just as it was getting functional ("Oh, wait! We need to get 5 departments and 3 unions to sign off on this"), but I definitely learned a lot.
vuolsi così colà dove si puote
ciò che si vuole, e più non dimandare
--The answer to Minos and any question of "Why are we doing it this way?"
|
|
|
|
|
Where there not solution already developed in this space by s/w companies ? example ()[^]
[^]
i guess its a matter of budget..
Caveat Emptor.
"Progress doesn't come from early risers – progress is made by lazy men looking for easier ways to do things." Lazarus Long
|
|
|
|
|
Budget and time. The primary contractor (a Dept. of Energy nuclear cleanup project) had taken over the system that was already in place as part of the original requirement. The site had also been running for about 50 years prior, so they couldn't just dump the historical data.
Spending big $$$ to do a migration (with all the sign offs from all parties involved) was going to bust the budget, as well as take too long. Instead, they had the idea that we could just build an overlay on the existing system that would automate the work previously done by all the managers and foremen.
It wasn't really a horrible idea, except that all the people who knew what needed to happen (the rules, restrictions, and such) had either left the site at the change over or were not involved in fleshing out the requirements. So it was a like a fractal rabbit hole--go down one, and you only see hundreds more inside.
vuolsi così colà dove si puote
ciò che si vuole, e più non dimandare
--The answer to Minos and any question of "Why are we doing it this way?"
|
|
|
|
|
It would be a challenge to get a new system in place, considering the change management needed.
Unless it is so simple a child could do it....anyway.Migrations would be costly , better to
park the existing system and cut over to a new one..but the new one should off course meet
all the requirements.....coming out of the rabbit hole with sanity intact...and doing
all this with JavaScript...whew...do you drink brandy...rum...
Caveat Emptor.
"Progress doesn't come from early risers – progress is made by lazy men looking for easier ways to do things." Lazarus Long
|
|
|
|
|