I did this research about a year ago, and I can say that it is faster to use this.test() .
But it depends on what you need. I do not recommend adding unnecessary functions to jQuery if you only want to create an object for a specific "widget".
Simple object-oriented use:
function Car(name) { this.name = name; } Car.prototype.startEngine = function() {
Another thing you need to understand is the environment (I hope you understand what I mean). It is better to make a new space of variables and functions, so there are not many variables / functions to look for. Keep in mind that as a list - if it is smaller, it is faster to find things in it. So you create a new βspaceβ:
(function(){
JavaScript will try to find things in "local" space, and then try to find things in global space. Immigrate it like this:
|- global |- function aaa |- function bbb |- var myName |- your local space |- function ccc |- function aaa It will not overwrite global function with the same name |- var someVariable
As you can see, there are only 3 elements in the "local space", so itβs faster to find things.
Faster to call semi-global functions in local "spaces". And, if you want to make some objects-oriented objects, it is better and faster to expand the "object".
UPDATE If you want to remove your library of functions, you can group them inside an object. I do not think it will be slower.
var lib = { foo: function() {}, bar: function() {}, test: function() {} }; lib.test();
Taai
source share