开发者

If we don't like it for the presentation layer, then why do we tolerate it for the behavior layer?

开发者 https://www.devze.com 2022-12-26 09:27 出处:网络
Suppose CSS as we know it had never been invented, and the closest we could get was to do this: <script>

Suppose CSS as we know it had never been invented, and the closest we could get was to do this:

<script>
// this is the page's stylesheet
$(document).ready(function(){
    $('.error').css({'color':'red'});
    $('a[href]').css({'textDecoration':'none'});
    ...
});
</script>

If this was how we were forced to write code, would we put up with it开发者_运维百科? Or would every developer on Earth scream at browser vendors until they standardized upon CSS, or at least some kind of declarative style language?

Maybe CSS isn't perfect, but hopefully it's obvious how it's better than the find things, do stuff method shown above. So my question is this. We've seen and tasted of the glory of declarative binding with CSS, so why, when it comes to the behavioral/interactive layer, does the entire JavaScript community seem complacent about continuing to use the kludgy procedural method described above? Why for example is this considered by many to be the best possible way to do things:

<script>
$(document).ready(function(){
    $('.widget').append("<a class='button' href='#'>...</div>");
    $('a[href]').click(function(){...});
    ...
});
</script>

Why isn't there a massive push to get XBL2.0 or .htc files or some kind of declarative behavior syntax implemented in a standard way across browsers? Is this recognized as a need by other web development professionals? Is there anything on the horizon for HTML5?

(Caveats, disclaimers, etc: I realize that it's not a perfect world and that we're playing the hand we've been dealt. My point isn't to criticize the current way of doing things so much as to criticize the complacency that exists about the current way of doing things. Secondly, event delegation, especially at the root level, is a step closer to having a declarative behavior layer. It solves a subset of the problem, but it can't create UI elements, so the overall problem remains.)


**Why for example is this considered by many to be the best possible way to do things: $(document).ready(function(){ $('.widget').append("..."); $('a[href]').click(function(){...}); ... }); **

Because this isn't simply behaviour. The proper way to execute the example you provide is with plain html. What you describe is appropriate for layering of additional behaviour in the manner of progressive enhancement. This is inherantly complex. This is the behaviour of behaviour.


JavaScript is tolerated only because it's everywhere. If it were actually a good language then everybody and their dog wouldn't be using it to create esoteric DSLs that try really hard to make you forget you're using JavaScript.

0

精彩评论

暂无评论...
验证码 换一张
取 消