0

I just started my very first JavaScript tutorial couple of days ago. I've come to the point where they explain the use of libraries. Two questions in mind thus far:

  1. getElementById is implemented as $("#id") in jQuery and as $("id") in Prototype. Why the inconsistency?

  2. If both libraries are included, how can the browser decide which object should be returned for $("")?

3
  • 2
    You most likely wouldn't use both jQuery and Prototype together as they do a lot of the same things. Commented Jan 17, 2014 at 19:32
  • @meagar What if I need to use both in my code? Commented Jan 17, 2014 at 19:32
  • 2
    api.jquery.com/jquery.noconflict Commented Jan 17, 2014 at 19:32

3 Answers 3

1

The browser doesn't "decide", whichever one you include second overwrites the first.

In situations where two libraries are fighting over $, use jQuery instead:

jQuery('#id') // same as $('#id') when $ == jQuery

You can then return control over $ to Prototype with jQuery.noConflict.

As for the inconsistency, they're different libraries written at different times by different people, with no intention of making them compatible. Consistency was never a consideration.

Sign up to request clarification or add additional context in comments.

Comments

1

1. Why the inconsistency?

The two libraries were implemented at different times, by different teams, and do very different things. Why would you expect consistency?

2. If both libraries are included, how can the browser decide which object should be returned for $("")?

That depends on exactly how you include them. However, jQuery has facilities to avoid conflicts like this, see jQuery.noConflict. It's been some time since I've used prototype, but it may have similar functionality, as well.

2 Comments

Prototype extends the prototypes (hence the name) of a number of low-level constructs in JavaScript itself. It's very much in the philosophy of Ruby, with its open classes, to make the language do what you need, rather than just using it as is. As a result, if another library expects the usual behavior, and relies on a broken interpretation of it (like using for…each to iterate over an array) then you will get inconsistent results. jQuery can deal with Prototype's use of the $ symbol using noConflict(). Not all jQuery plugins are written well enough to deal with the rest.
Prototype: $('itemId') -> single item or false. $$('.className tag whatever selector') -> array of matched items or empty array. jQuery: $('#itemId') or $('whatever selector') -> object containing matches.
0

This link will help you to understand handling conflicts while using jquery with other library like prototype http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.