I have created a ton of Image objects in javascript and put them all in an array. However, some of these objects have a mouseover() and mouseout() property, and some do not.
Is there a way to determine if the object I'm referencing has those functions defined or not?
I've tried
if (typeof obj.mouseover !== 'undefined')
but if I never even declared
mouseover = function() { ... }
on that object, then the code just breaks right there.
So I'm looking for a way to determine if I even added 'var mouseover = function() { ... }' on each object.
Of course, I could go through and make sure EVERY object gets mouseover and mouseout created, even if not set as anything, but that feels like an unnecessary pain if there's another way to just detect if that was set in the first place.
Thanks.
You can check that the method exists on the object via Object.hasOwnProperty('someMethodName')
Mozilla dev link
Use reflection. Eg.:
Javascript Reflection
It is then easy to write a function like:
DoesMethodExist = function (object_, methodName){...}
that iterates through all the method names of object_ and matches them with methodoName.
Related
I want to make an UI object that saves references to DOM objects and also some UI strings, so I can use it all over my code and can handle name changes easily. Kind of like this:
var UI = {
DOMelem0: document.getElementById('imUnique'),
DOMelem1: document.getElementById('imSpecial')
};
However, I think that everytime I would access DOMelem0 (by calling UI.DOMelem0), for instance, I'd be calling the getElementById() function, is that what happens?
Or is it no different than storing the elem in a scoped variable? (Like so: var elem = document.getElementById('cow');)
I'm worried about any performance issues this might cause if I were to have lots of UI elements, although I guess they'd be minimal. Either way, I wouldn't want to be calling the DOM method all the time.
Thanks.
Calling UI.DOMelem0; will not call document.getElementById('imUnique').
document.getElementById('imUnique') is only called when you first create the UI object.
I'm currently creating a project in which every visible element, is represented by javascript object.
I need a method within this object, which will let me destroy it.
There's an example of that object :
var example = function(some_args){
var self = this;
var references = {}; //this holds references to inputs within given view
this.createView = function(){
//here I`m doing "stuff" like filling innerHTML of container
//creating event delegate, etc.
}
this.destroy = function(){
self.elements["box"].parentNode.removeChild(self.elements["box"]); //box is a reference to container element
self.elements = null;
delete self;
}
Now, my question is : am I doing everythig what I have to in order to COMPLETLY destroy this object?
I`m not holding any other references to objects or elements.
EDIT:
I see that some of You do not undestand my question.
Barmar got it right, for which I am gratefull :).
I`m aware of GC, it is just easier to write "I am destroying object" than "I am removing last reference to object, so GC could take care of it" :)
To be specific. Considering that I am removing last reference to object, inside function which is part of this object - is there anything else I should take care of? Or my code is completly fine, and object will be considered as garbage?
You don't handle lifetime of an object in JS explicitly.
So for the given question:
am I doing everythig what I have to in order to COMPLETLY destroy this object?
the only answer is:
You cannot do that, since an object in JS can only be destroyed by a GC, which you cannot interact with. When an object is reachable - then it's alive. When it's not - then it's a "Schroedinger's object".
Javascript has a garbage collector, so you don't need to destroy the object. Also you can't
I am using code lines like the following in order to fetch data from an intranet website:
util.setProp(obj, "firstNameOld", $(msg).find('#fname_a').text());
Now I have another function in the same file where I want to use the above again, resp. the value of that object - currently I am hard-coding this ('Test') for test purposes:
util.setProp(obj, "firstNameNew", 'Test');
How can I pass the value from the firstNameOld object in one function to the firstNameNew object in another function ? If a solution with global variables is better here than this would work as well.
Many thanks for any help with this, Tim.
I've never used the framework that includes util But I imagine that if there is a setProp() then there has to be a getProp() or something similar.
If so, you could do something like
util.setProp(obj, "firstNameNew", util.getProp(obj, "firstNameOld"));
This also relies on the assumption that you want to copy from two properties in the same object.
If not, then pass the desired source object in the getProp() call.
My guess is that functions (or properties) are called "firstNameOld" and "firstNameNew", so the first time you get it from selector, second time you want to do the same.
Try to use the local variable like that:
var text = $(msg).find('#fname_a').text();
//
util.setProp(obj, "firstNameOld", text);
//
util.setProp(obj, "firstNameNew", text);
I have written this code (this is a snippet) that doesn't seem to be working. I have isolated it to here.
grab = window.document.getElementById;
grab("blueBox") // i.e. grab("blueBox").onclick [...]
Is it possible to create references to native function in javascript. I am doing something with the grabbed element, I just left it out for example. The grab function doesn't seem to work.
I am using FireFox's most recent version
The way you're doing it will mess up the assignment of the this value for the function.
grab = window.document.getElementById;
grab("blueBox") // i.e. grab("blueBox").onclick [...]
here this will be the global object. Try:
grab.apply(window.document, ["blueBox"])
or in newer browsers:
grab = window.document.getElementById.bind(window.document);
to get directly define what this will be.
The first step here is always the JavaScript console. Firebug is your friend. Tell us the error message if it doesn't mean anything to you.
In the mean time, here is a workaround:
var grab = function(id) { return window.document.getElementById(id); }
function grab(id) {
return window.document.getElementById(id);
}
grab("blueBox");
The reason is because the function getElementById is not being called as a method of document, so its this keyword doesn't reference the right object. Using call as suggested in other answers shows that when this references the document, getElementById works.
Say I have the following code:
$('#someid').click(function(event) { myFunction(event); });
function myFunction(event)
{
// do something with event
}
And I would like to test myFunction() with an eval statement, without doing something like using eval('$("#someid").click()');
Is there a way to manually create an event object in javascript that would work with an eval statement?
Also if myFunction is only used as an event handler, can it always assume event is not null?
Well, assuming that's JQuery at the top, the event object shouldn't ever be null.
The event object is a completely different critter between IE and everybody else so unless JQ 100% normalizes the object (I think it builds its own event object from the looks of it), it may still vary in properties that JQuery doesn't use between browsers. To manufacture your own event object I guess I would just use a for x in loop to see what's inside and build an object hash that simulates that.
So on this page something like:
$('#adzerk1').click( function(event){
console.log('fakeEvent = {');
for(x in event){ console.log( x + ':' + event[x] + ',\n')}
console.log('}');
} );
$('#adzerk1').click();
will return all properties and their values for that event in the console box of firebug. You'd have to make it recursive and test to get all the innards of properties of event that are objects themselves, however. Otherwise I've set it up so that all you would have to do is cut and paste the results into your JS and then delete the last comma before the closing curly bracket.