If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Strict JavaScript and "use strict"

A few weeks ago, someone suggested that my PHP methods weren't quite acceptable for programming correctly in JavaScript. So I set out to learn how to program strictly in JavaScript.

I'm still learning JavaScript, but I'm learning more everyday. I figured learning to be more strict would help me to learn faster. Obviously I still need to learn more about DOM standards and browser compatibility.

My question is where can I find exact standards on programming using the "use strict"; statement? I read somewhere that browser writers are only just beginning to look at making them compatible with this statement, so there's no way for me to test my strictness. And to make matters worse, JSLint doesn't even find "strict" errors. It only checks for the use of the "use strict"; statement.

For example, I was trying to practice some simple problem solving skills and decided to write a JavaScript version of the binary clock app on the iPhone, just for fun. I figured it would be a good chance to figure out strict code, so I put it all in JSLint.

I plan to add functionality to it for dates, epoch time, displaying actual time, color choices, etc. That way, I'll be building a program and building good coding form at the same time:

Unfortunately it is not a matter of strict coding, but a matter of crossbrowser coding. Even there are a JavaScript standard (see ECMAScript) and DOM standard (see W3C DOM) no browser follows entirely the standards. IE is the champion of deviations. The good news is that those deviations are not quite so many, or better say many of them are not so important.

Here's an example, from your code:

Code:

document.getElementById(digID).setAttribute("class", bcclass);

For IE, class is a sort of reserved word, thus it will use className instead of class. Fortunately, there is a simple crossbrowser solution: DOM 0 syntax:

Code:

document.getElementById(digID).className= bcclass;

Same with the addEventListener and attachEvent. Your crossbrowser function is OK, in case you want to add another function to be fired by the event. But if you need simply to create and add a new one, simply use the old DOM 0 trusty syntax:

Code:

element.onload=function(){
// statements, functions, whichever
}

A good and comprehensive site where you can see all the differences (not only for JavaScript and DOM, but for CSS and even HTML as well):

leaving out the most powerful features of javascript like eval and with makes it a lot simpler to pre-optimize and/or JIT compile.

Firstly, powerful is a unusual ephitet. I never found a use for with, and very rarely for eval or its cousins.
I didn't think eval is totally gone? I thought you just couldn't declare variables with it.

Apparently "eval" has some security issues if you don't trust the source of your JSON. That being said, "eval" is great for processing JSON. In fact, I think it's the only way to get JSON to produce results. So I agree that it is powerful.

See, I thought that strict mode had to do with the quality of the code and gave more warnings for standards and security. I guess you're saying that's not the case, but that it's more of a reduction in functionality for security reasons. That makes sense. I guess I'll just stick to JSLint's requirements.

I had emailed Douglas Crockford to find out exactly what JSLint does with strict mode, since I read that JSLint doesn't find run-time strict mode errors (yeah I bothered the poor guy), and all he said was:

"The 'use strict' directive is a feature of ES5, which removes or redefines some of the bad parts."

I thought that didn't said much, but now that I've heard the extended version and read a little about JSLint, I see that it actually says a lot.

Firstly, powerful is a unusual ephitet. I never found a use for with, and very rarely for eval or its cousins.
I didn't think eval is totally gone? I thought you just couldn't declare variables with it.

I don't think it's a security concern, it's a speed concern. There are plenty of ways to run code without eval or Function... ES-CP doesn't have eval or with either... last i heard, eval was a syntax error under "use strict", but perhaps they backed down or i/they are mistaken.

i left eval alone for a long time. As i became more interested in expanding the language, i've realized that eval is about the only way to do so. Function() just doesn't have the scope. I hope eval still works, i'm just getting started making language extensions...

Performance is not everything, and with TraceMonkey and V8 type technologies becoming the norm, we should be able to get away with a lot more than we've become accustomed to.

for example, this expanded syntax can work only by using eval (and only by using it inside the closure of the constructor):

efficient? not really, but not as terrible as you might think...
neat? i think so, this is just one flavor; labels can do anything you code...

with can also be used simulate classes, smuggle closures, and speedup code by eliminating long-winded property lookups.
If a needed prop is behind a getter, you can't simply use a variable cache, you need with...

given the persistent bad-mouthing, i am just getting into experimenting with with and eval over the past year.

Don't get me wrong;
a "use strict" function library will likely transform a bitmap image quicker than a standard one would (for now),
but, let's not proclaim "use strict" to be anything other than castrated ES5.