开发者

What is Javascript collision?

Any开发者_StackOverflow best practices around it?


JavaScript collision is when you have two global objects with the same name, and one overwrites another. For example, you might reference two libraries that both use a function named $ at the root object (window) for a query function. The idea is that you should use as few global objects as possible. The best way to do this is to create a namespace for any JS you write, just like any other language:

var myApplication = {};

And then add any subsequent functions / objects inside the namespace:

myApplication.init = function () {

}


google sometimes better than stackoverflow

http://javascript.about.com/od/learnmodernjavascript/a/tutorial22.htm


I think it is a concept which detects when two objects collide, this technique is especially useful when creating javascript-based games.

This also refers to collision of different javascript libraries used in one page for example jquery and prototype used in one page and nothing works because of collision usually because of popular $ sign.


Javascript has first class, lexically scoped functions, in other words in side of a function, when you call a variable, it checks itself for an instance of that variable, then checks it's parent function.

<script>
  var foo = "test1";
  document.write(foo+"\n"); //test1+ a linebreak
  (function(){
    document.write(foo+"\n"); //test1+ a linebreak
    var foo  = "test2";
    document.write(foo+"\n"); //test2+ a linebreak
  })();
  document.write(foo+"\n"); //test1+ a linebreak
</script>


I thought it was something that happens when a Mummy Javascript and a Daddy Javascript, who love each other very much want to eval a baby Javascript.

My teacher wasn't very clear on this point however ...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜