Despite excessive googling I just don't get why my function doSomething does nothing in the situation below. Any idea why it doesn't work?
Many thanks, Gordon
var arrAttend=new object();
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
It's a typo, you should use new Object (capital O). Or use an Object Literal:
var arrAttend = {Blob: 'hello'};
function doSomething() {
alert (arrAttend.Blob);
}
Two problems :
object isn't defined
you don't call your function
Try this :
var arrAttend= {}; // that's the simplest way to create a new javascript object
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
doSomething();
Note that the first kind of error is very easily found when you look at the console : an error is displayed. I'd suggest you to use developer tools (for example Chrome's ones) so that you don't develop in the blind. BTW you'd see that using console.log instead of alert is most often more convenient.
Try this :
var arrAttend=new Object();
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
There's typo error in your code. And an object should be used like follow -
var arrAttend= {
name:'Blob'
};
function doSomething() {
alert (arrAttend.name);
}
doSomething();
Try this:
// create object
var arrAttend=new Object();
arrAttend["Blob"]='hello';
function doSomething() {
alert (arrAttend["Blob"]);
}
// call function
doSomething();
Related
I am newbie in js and I want to override/overwrite some fullcalendar functions from another script (my-fullcalendar.js) to make some changes in it for myself. for example function names are :
formatRange and oldMomentFormat.
formatRange is accessible from this.$.fullCalendar.formatRange but oldMomentFormat is not accessible via this kind of chain. But even when I do something like this in my-fullcalendar.js:
;(function () {
function MyformatRange(date1, date2, formatStr, separator, isRTL) {
console.log( "MyformatRange");
//other parts is exactly the same
// ...
}
this.$.fullCalendar.formatRange=MyformatRange;
console.log(this);
})();
nothing happens because no log is generated and even line by line tracing does not pass from here. but when observing "this" in console log MyformatRange replaced by original formatRange.
another problem is how can I override/overwrite oldMomentFormat function which is not in window hierarchy to access (or I can not find it) ??
OK, let's simplify the problem. In essence, you have this situation:
var makeFunObject = function () {
var doSomething = function (msg) {
console.log(msg);
};
var haveFun = function () {
doSomething( "fun!");
};
return {
doSomething : doSomething,
haveFun : haveFun
};
};
In other words you have a function that is creating a closure. Inside that closure are two "private" functions, one of which calls the other. But both functions seem to be "exposed" in the returned object.
You write some code:
var myFunObject = makeFunObject();
myFunObject.haveFun(); // fun!
Yep, seems to work just fine. Now let's replace the doSomething function in that returned object and call haveFun again:
myFunObject.doSomething = function (msg) {
console.log("My new function: " + msg);
};
myFunObject.haveFun(); // fun! <== wait what?
But wait! The new replacement function is not being called! That's right: the haveFun function was expressly written to call the internal function. It in fact knows nothing about the exposed function in the object at all.
That's because you cannot replace the internal, private function in this way (you cannot replace it at all, in fact, not without altering the original code).
Now draw back to the FullCalendar code: you are replacing the external function in the object, but the internal function is the one that is called by every other function inside FullCalendar.
I realize this is an old question, but I was butting my head against this same problem when I wanted to override the getEventTimeText function.
I was able to accomplish this, from inside my own JS file, like so:
$.fullCalendar.Grid.mixin({
getEventTimeText: function (range, formatStr, displayEnd) {
//custom version of this function
}
});
So, in terms of the function you were trying to override, you should be able to do it with:
$.fullCalendar.View.mixin({
formatRange: function (range, formatStr, separator) {
//custom formatRange function
}
});
Note: Make sure this runs before where you actually create the calendar. Also note that you need to make sure to override the function in the right place. For example, getEventTimeText was in $.fullCalendar.Grid, while formatRange is in $.fullCalendar.View.
Hopefully this helps other people who end up on this question.
In my jQuery scripts, when the user closes a menu with an animation, I have to call a function after the closing animation is finished. I want to assign this function dynamically by calling a function openStrip() with a parameter. My code looks like:
var FUNCTION_JUST_AFTER_MENU_CLOSE = function(){};
function openStrip(stripId){
FUNCTION_JUST_AFTER_MENU_CLOSE = function(){
createStrip(stripId);
});
}
if I call openStrip("aStripId"), I expect FUNCTION_JUST_AFTER_MENU_CLOSE to be:
// #1
function(){
createStrip("aStripId");
}
whereas my current code gives:
//#2
function(){
createStrip(stripId);
}
i.e, the parameter passed to the function openStrip() is lost while assigning the function() to the variable FUNCTION_JUST_AFTER_MENU_CLOSE.
How can I avoid this.
EDIT: I discovered that my code is actually working. The problem was elsewhere. I got confused because when I looked at Chrome's debugger, it was showing me the function definition as is (#2 in above). But when it actually went down executing that function later in the code, it did evaluate the values of the passed argument, and endedup executing #1.
Thanks for the answer though. I am marking it correct because that is perhaps a better way of assigning the function.
The best way is to return a function, from openStrip like this
function openStrip(stripId) {
return function() {
createStrip(stripId);
};
}
For example,
function openStrip(stripId) {
return function() {
console.log(stripId);
};
}
openStrip("aStripId")();
# aStripId
openStrip("bStripId")();
# bStripId
You can even assign the function objects returned to different variables and use them later on
var aStrip = openStrip("aStripId");
aStrip();
# aStripId
aStrip();
# aStripId
While I browse for some JS Code, I found the code.
Sample code:
arg = "TEST ALERT MESSAGE";
MyFunction(arg);
function MyFunction()
{
alert(arg)
}
Above MyFunction() is given the alert message, but there is no parameter to receive the incoming arugment.
But when I change the 'arg' variable(in the function and out side the function 'arg'), it is not working.
How it is possible?
Thanks in advance
Variable scope in javascript doesn't work the way you think.
in order to make that variable 'private' try:
var arg = "not private";
function privateMSG(){
var arg = "private";
function alerter(){
alert(arg);
}
alerter();
}
MDN has a much better explanation in the Nested functions and closures section
please can you tell me why this wont work.
If I call s.A why wont the alert show.
var s = {
A: function () { alert("test A"); },
B: function () { alert("test B"); }
};
s.A;
thanks
Try
s.A();
A is a function. If you just say s.A; all you're doing is emitting the reference to what A is, e.g. if I whack s.A; into Chrome's javaScript console I get the following:
Notice how all it did was output the function definition?
Now, if I say `s.A();' I get what you originally expected - it fires the function:
see it working on jsfiddle. you'll have to add braces to s.A to make it a function-call.
s.A();
You're returning a reference to the function, but it's not being called. To do so, add the braces after s.A:
s.A();
I'm trying to mimic static variables on a JavaScript function, with the following purpose:
$.fn.collapsible = function() {
triggers = $(this).children('.collapse-trigger');
jQuery.each(triggers, function() {
$(this).click(function() {
collapse = $(this).parent().find('.collapse');
})
})
}
How do I save the "collapse" object so it doesn't have to be "found" on each call? I know that with named functions I could do something like "someFunction.myvar = collapse", but how about anonymous functions like this one?
Thanks!
You can save your variable in the function, using either functioName.myVar = value or arguments.callee.myVar = value if you don't have the current function name.
arguments.callee is the current function you are in.
For anonymous function you could use a function that returns a function.
For instance:
var myAnonymousFunction = (function(){
var myFirstStatic = $("#anElement");
var anotherStatic = true;
return function(param1,param2) {
// myFirstStatic is in scope
// anotherStatic also
}
})();
Should work like a charm and you're assured initialisation code for statics is only executed once.
It seems that a better answer to this question is found elsewhere on Stack Overflow.
In short, you can actually give anonymous functions names without polluting the namespace, yet still allow self-referencing.
mything.prototype.mymethod = function myKindOfFakeName() {
myKindOfFakeName.called = true;
}
As long as you're assigning the function to a variable like that, you should be able to access it as $.fn.collapsible, and thus assign variables as $.fn.collapsible.myvar.