Implementing Prototype-style class structure with jQuery
-
29-09-2019 - |
Question
I've been using heavily Prototype's way of defining classes and subclasses:
// properties are directly passed to `create` method
var Person = Class.create({
initialize: function(name) {
this.name = name;
},
say: function(message) {
return this.name + ': ' + message;
}
});
// when subclassing, specify the class you want to inherit from
var Pirate = Class.create(Person, {
// redefine the speak method
say: function($super, message) {
return $super(message) + ', yarr!';
}
});
var john = new Pirate('Long John');
john.say('ahoy matey');
I'm working with my 2.3.8 Rails app and planning to use Highcharts (charting JS library). My problem is that Highcharts is dependent on jQuery (or MooTools).
Is there a way to modify Prototype-style defined classes and subclasses into jQuery format? Or should I change my existing classes into plain JavaScript? Thanks for your help!
Solution
You can use both Prototype and jQuery together on the same page by using jQuery.noConflict(). Is there a reason that you only want to use one? Use Prototype for most things since you seem comfortable with it and jQuery for the plug-ins that require it...
// during loading
jQuery.noConflict();
// later when you want to manipulate things
jQuery('div.hideme').hide(); // jQuery
$('#somethingElse').hide(); // Prototype
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow