var myNamespace = {
dateController: {}
};
myNamespace.dateController = function(callback) {
this.callbackfunction = callback;
try {
[this.callbackfunction]();
} catch (e) {
alert(e);
}
};
function displayDate() {
alert("displayDate");
myNamespace.dateController("displayDateFromController");
};
function displayDateFromController() {
alert("In displayDateFromController");
};
This piece of code is giving me TypeError: ["displayDateFromController"] is not a function error. What could be root cause and possible solution to this issue.
Why dateController not able to identify displayDateFromController as function.
I have tired this on
http://www.w3schools.com/js/tryit.asp?filename=tryjs_events
You need to pass the actual function to the datecontroller method instead of the String.
var myNamespace = {
dateController: {}
};
myNamespace.dateController = function (callback)
{
this.callbackfunction = callback;
try{
//remove [] surrounding function
this.callbackfunction();
}
catch(e)
{
alert(e);
}
};
//Declare this method prior to displayDate
function displayDateFromController()
{
alert("In displayDateFromController");
};
function displayDate()
{
alert("displayDate");
//Pass function instead of string
myNamespace.dateController(displayDateFromController);
};
displayDate();
Working Example: http://jsfiddle.net/RDMHV/
If you still need the flexibility of a String:
var myNamespace = {
dateController: {}
};
myNamespace.dateController = function (callback)
{
this.callbackfunction = this[callback];
try{
this.callbackfunction();
}
catch(e)
{
alert(e);
}
};
myNamespace.displayDateFromController = function(){
alert("In displayDateFromController");
};
function displayDate()
{
alert("displayDate");
myNamespace.dateController("displayDateFromController");
};
displayDate();
Working Example http://jsfiddle.net/RDMHV/1/
You have to remove the brackets around the call :
try{
this.callbackfunction();
}
catch(e)
{
alert(e);
}
and to pass the function without the quotes :
function displayDate()
{
alert("displayDate");
myNamespace.dateController(displayDateFromController);
};
Related
Apps Script
function Docs() {
this.getActions = function(p) {
console.log("get action calling")
}
I need to call the getActions function in Docs funtion
function Docs() {
this.getActions = function(p) {
Logger.log("get action calling")
}
this.callActions = function () {
this.getActions();
}
}
function test() {
var docs;
docs = new Docs();
docs.getActions();
docs.callActions();
}
I'm trying to call a function inside a function outside the main one:
var myFunction = function myFunct(element) {
function tryIt(){
alert("hello");
};
return{
tryIt: tryIt
}
}
And I'm try to call "tryIt" function outside myFunct with
myFunction.tryIt
But it doesnt work.
how can I do this?
First, you need is to call the function and then call the function of the returned object of the property tryIt.
var myFunction = function myFunct(element) {
function tryIt(){
alert("hello");
}
return {
tryIt: tryIt
};
};
myFunction().tryIt();
// ^^ calls myFunction
// ^^^^^^^^^ returns object with function tryIt
// ^^ calls tryIt
function myFunct() {
function tryIt() {
console.log("hi");
}
return {
tryIt: tryIt
};
}
var foo = myFunct();
foo.tryIt();
I want to do something like this:
var build= (function(){
//my function body
})();
function test(){
//somthing then call build
build() //i want to call build function again in my code
}
How can I do this?
I tried this in angular:
var buildRoot = (() => {
$SubNode.get({
TypeID: vendorAdminService.NodeType.Category
}, function(data: vendorAdminService.IGetNodeParameters) {
$scope.ProductTree = data.TreeNodeModelItem;
$scope.AjaxLoading = false;
}, function(err) {
// alert(err)
})
})();
$mdDialog.show(confirm).then(function() {
$Category.Remove(node.ID)
buildRoot
}, function() {
});
but it does not work.
Anybody can guide me??
You need to return a function in your IIFE.
If you IIF is not trivial and has many functionalities you could also consider using Reveal Module Pattern.
var build = (function() {
var f = function() {
console.log('hello');
};
f();
return f;
})();
function test() {
build();
}
test();
Just use a named function.
Your IIFE needs to return a function, for later calling. But then is no need for an anonymous function.
function build() {
//my function body
}
or
var build = function () {
//my function body
};
var build = (function() {
var init = function() {
// magic code
};
return {
init: init
}
}());
function test() {
build.init()
}
test();
You include all your functionalities inside your build object, and you'll be able to call them as soon as you return them from inside that object. This effectively is called the revealing module pattern
For more information, read this
I see that there are missing semi-colons ";"
$mdDialog.show(confirm).then(function() {
$Category.Remove(node.ID);
buildRoot();
}, function() {
});
How to create a function that accepts the parameter as a function?
I have this function and I want to call it with parameter of a function also:
function ShowDialog(OnSubmit)
{
var _result = OnSubmit();
alert(_result);
}
This is what I want to call:
ShowDialog(
{
OnSubmit: function () {
return "Hello World";
}
}
);
this will alert "Hello World in browser"
Call:
ShowDialog(function(){
return "Hello World";
});
Then your function can remain as:
function ShowDialog(OnSubmit)
{
var _result = OnSubmit();
alert(_result);
}
JavaScript has first-class functions, so you can just pass the function reference directly, you do not need an object:
function showDialog(callback) {
var result = callback();
alert(result);
}
function bye() { return "bye!" };
showDialog(function() { return "Hello!" });
showDialog(bye);
var greetings = { ciao: function() { return "Ciao!" } };
showDialog(greetings.ciao);
Of course you can also pass the full object, in that case you need to modify the function as follow:
function showDialog(options) {
var result = options.onSubmit();
alert(result);
}
And then:
showDialog({
onSubmit: function() { return "Hello!" }
});
showDialog({
onSubmit: bye
});
You could also implement a function that accepts both object and callback:
function showDialog(options) {
var result = typeof options === 'function' ? options() : options.onSubmit();
alert(result);
}
showDialog({
onSubmit: bye
});
showDialog(bye);
Hope it helps.
Try this instead:
function ShowDialog(param)
{
var _result = param.OnSubmit();
alert(_result);
}
ShowDialog({
OnSubmit: function () {
return "Hello World";
}
});
I am using the following code below and essentially it takes an element as input (i.e.):
$(#someDiv).Calculator();
but the problem is that I want to call this plugin's function within itself, but I don't know how to get an object/handle to itself to call the function. caching off "this" does not have the proper reference.
(function ($) {
jQuery.fn.Calculator = function () {
var selectedObjects = this;
var control = $(selectedObjects[0])[0];
$("#btnCalculate").click(function(){
// this is where calculate needs to be called
someObject.calculate(...);
});
return {
calculate: function (value) {
// do some code
return selectedObjects;
}
};
}
})(jQuery);
Any ideas/direction would be of great help! Thank you all!
Try
(function ($) {
jQuery.fn.Calculator = function () {
var selectedObjects = this;
var control = $(selectedObjects[0])[0];
$("#btnCalculate").click(function(){
// this is where calculate needs to be called
calculator.calculate(...);
});
var calculator = {
calculate: function (value) {
// do some code
return selectedObjects;
}
};
return calculator;
}
})(jQuery);