FAQ: Functions - Helper Functions
object oriented - What is the recommended approach for helper functions in JavaScript? - Software Engineering Stack Exchange
jquery - Creating an object_helper with functions properly in javascript - Stack Overflow
Where to put Helper Functions
Videos
First JavaScript doesn't have Classes.
Second, your third option seems more rational to me, but it highly depends to your requirements as well. Also you should not be much worried about exposing the helper function. The pros of the solution totally justifies the compromise to me.
Third, your time as a developer is valuable; Don't make trivial tasks hard to implement, time consuming and more pron to human-errors. Simplicity of source code is a great feature itself.
Statics belong on the Function IMO, in your case you have a private static, so ...
Panes._createPane=function(pane){}
You must add a function into your helper and use the variable of parent function.
var person_helper = function(person) {
var parent = this;
this.name = person.name ;
this.isHisNameIs = function(name) {
if(name == parent.name)
console.log('OK');
else
console.log('NOP');
}
}
http://jsfiddle.net/H4RsJ/6/
In my opinion it is not such a good idea to create a person_helper. Instead you should create a Person prototype, read this for more information.
You should have members such as Name and Alive and you can implement your functions based on your requirements.
Ok I know you can you can create a class in Javascript and then give it a function which is accessible via an instance of that object
class Fruit{
eat(){
console.log("Chomp!");
}
}
var apple = new Fruit();
//prints Chomp
apple.eat();This works.
Strangely though, I have found that if you declare an additional function in that class and call that function within eat, it will NOT get executed.
Eg:
class Fruit{
eat(){
helper();
console.log("Chomp!");
}
helper(){console.log("Hi this is the helper")}
}
var apple = new Fruit();
//prints Chomp
apple.eat();When you try this, it still only prints Chomp. Which means the helper is not being called. How come? Haven't I defined it properly? I initially defined it as " function helper() " but apparently you are not supposed to use the function keyword inside a class.
What have I done wrong?
Callbacks are the functions that are passed as a function argument and are performed after a particular event such as resolving of a promise.
Helper functions are the normal functions that are called at any point of time when the code execution is taking place. Mostly, helper functions are wrapped inside another function.
Example of callback function:
const fun = (callback) => {
setTimeout(callback, 3000);
};
fun(() => {
console.log('callback function');
});
Example of helper function:
const factorialOfNNumbers = (...numbers) => {
const helperFact = (n) => {
if (n ===1 || n === 0)
return n;
return n * helperFact(n-1);
};
return numbers.map(n => helperFact(n));
};
console.log(factorialOfNNumbers(2, 3, 4));
They are both functions, but the difference I suppose is how they are referenced and/or used.
Helper functions are just called "whenever" when they are in scope. Callbacks are typically passed elsewhere and invoked with some parameters.
I hope the below snippet explains it.
// Just some function, what does it do?
const someFunction = (...args) => console.log("I'm a helper function", ...args);
const useAsHelperFunction = () => {
// Called directly
someFunction('used as helper function');
};
const useAsCallback = callback => {
// invoking callback
callback('used as callback function');
};
useAsHelperFunction();
useAsCallback(someFunction);