Do many programmers remember most of the object properties and methods or do they use IDE or references to find those specific objects. I'm starting to learn Javascript and seeing all the different type of objects available can be depressing. :(
Well, I've been working with JS for three years and have a great experience here. But! I still have no really acceptable answer to the following question:
What is the principle difference between declaring methods/properties in the constructor function body and via prototypes.
Are there any real GURUs? Let's discuss the issue.
I have an object on the document element that allows for other components to register with it, i have a custom event something along$(document).bind("register",function(thechild)..So in the child object when they are created i call$(document).trigger("register",this);And indeed i get the DOM object. However i'm looking for the plug in object, i want to be able to call methods on the passed childobject and access it's Config.Does that make sense? How can i write a plug in that is applied to various objects that also registers itself with an 'overseer' object on the document element in such a way that i can allow that overseer object to call methods on any registered child objects?
I have been making slow progress on my goal of creating a Firefox extension. My plans are to control the modifications this extension affords by using mouse wheel movement plus the Alt key. So it is vital for me to understand the Firefox mouse wheel event and how to cancel the default action and replace it with my own. Searching the web to find out how to achieve this the best source thus far has little explanation but code that achieves much of what I want. The code is posted below and I have more than one question as to how it works.
In this code the word "wheelDelta" seems to appear out of nowhere used as if it is a property. So I put it to you Can an event like DOMMouseScroll have properties and methods? If it can, can you either referrer me to a reference as to what they are or tell me what they are? I will need to pick the Firefox only code out from the below code so that would certainly help.
I am also not understanding the line "if (delta)" since delta is declared as a variable in the function we're the if delta condition is queried. It seems to me how could you have delta returning anything else but the zero it is initiated to, so why is this "if (delta)" necessary. Well this delta variable also seems to be changing to create the change in the text in the div tag with the delta ID so this is confusing me to. Code:
how to add extra properties to html elements as I was storing data in html attributes. After looking at some others code including Raphael and this addEvent code. [URL] They seem to treat objects just like an array. obj[property] = value; This would have been extremely helpful to know previously as I have needed to be able to include variables in property names - but have resorted to making the whole thing a string and calling exec() on it.
Can assign a new function to a built-in object in Firefox:
But IE and Opera don't have a MouseEvent or HTMLElement that can be set up in the same way. Can you do this in IE or Opera, or just Firefox, and maybe Webkit?
I have a javascript function that needs to access methods of a java object(localTag). In my JSP I'm trying to include hidden fields for the Strings returned from the getter method calls of the object like so:
If I can do something like this, what is the correct syntax and how do I access this property in my javascript? When I have a hidden field that is just a String, I access it in the javascript like "document.getElementById("theString").value" and it works fine, but I can't seem to find how to access the String value of a method call.
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter?
The ECMAScript reference has a lot to say about the caller using Function.prototype.call or Function.prototype.apply and passing the correct this pointer for the context, but how does addEventListener determine the correct this pointer. Or does it just punt and pass the global context, thus making it impossible to refer to the object this in an object method used as an event listener?
I have more of a programme design related question here: I have an object with 2 methods, those two methods are supposed to be called repeatedly one after the other (e.g. by setInterval())
the problem with this code is that the execution time of those methods may increase depending on the processed data (an array with several hundred elements or more) so it may be, that the execution time of both methods exceeds the given repetition time. I could solve that by placing a call to the next function in each method, but I wonder if that is good design or not (tight coupling)
code for method2() is analogue Obj.prototype.method1 = function () { /* working code here */
When a custom object is created in javascript, then methods need to be defined for this object. Still every object has toString() method available to them, even if it is not defined. How is this method available to all the objects ? Is it inherited from some root object ?
Is sort() method available to all the objects ? What are the other methiods available to all objects ? Where can I get list of available methods ?
I am a little confused how the memory for objects is allocated in JavaScript. David Flanagan, in "javascript: The Definitive Guide," states that each property of a class takes up memory space when instantiated. So space is created for aMethod three times in this example: Code:
I'm attempting to understand the use of privileged methods when used with Object.create. I put together a quick demo (code below) that shows what happens when I use Object.create to create a new object based on one that has a privileged method. The outcome is not pleasant, as changing the value in the first object also changes it in the second. Unless I am reading Crockford's articles incorrectly, this makes Object.create almost useless for anything but objects that have only public members.
I've seen many JavaScript programmers use closures extensively to create private members, and that still holds to be a good programming practice. However I still can't find an elegant way to create inheritance in combination with closures in JavaScript, given downfalls such as the one I mentioned above.With all of that said I still think Crockford has a nice way of programming, creating factory functions that produce objects, staying away from the prototype property and making the language look more functional.
Here's the code to demonstrate what I'm referring to. Firebug needs to be enabled to view the console.debug output, otherwise convert them to alerts.
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {}
1) is getRule a local variable or global variable, as it has no var keyword, yet it is an inner function of Validation? So without var, I think global, but being an inner function, I think local. So I'm not sure which.
2) In this line of code: var rule = $.Validation.getRule(types[type]), getRule returns rules, which is just a local variable in Validation. I always see that you return functions, but how does returning a local variable that's just an object literal and not a function be able to return true or false? Now the value of rules is an object literal, and this object returns true or false. So we are basically allowed to use return keyword with local variables that are object literals and not functions?
3) In this line, is foo(age) being called, or is it just being assigned to bar OR is it being called and THEN assigned to bar: var bar = foo(age);
4) Now for the most confusing: age is obviously an object reference as opposed to a literal in the example. Does that make a difference in regards to closures? Note that I read a number of books, including JavaScript Programmer Reference and Object Oriented JavaScript and jQuery cookbook, which compare primitives vs reference types and how primitive types store directly in memory whereas reference tpyes reference memory, so if one reference changes, they all change where primitive remains ingrained. But when assigning a function as a reference like this, how does that affect the object "age" when passed into bar?
Code: function foo(x) { var tmp = 3; return function (y) { alert(x + y + tmp); x.memb = x.memb ? x.memb + 1 : 1; alert(x.memb); }} var age = new Number(2); var bar = foo(age); // bar is now a closure referencing age. bar(10);
i'd like to know objects properties and/or methods.
saying i do have an object "o" being part of the dom (a div or an ul...) how could i list (introspection) all the properties and methods attached to this object ?
i know it is possible in javascript but don't remeber how to ...
I am fairly familiar with the concept of Objects and their properties and methods, but javascript being object based as opposed to object oriented has me stumped on how to access an object's properties from an onclick event handler created for another object created within the original object.
In the example below, I have a constructor function called anyObj. to which I pass an object reference to an element.
anyObj has 3 properties and one function increaseWidth()
increaseWidth() creates a new button with an onclick event handler and this is where I have a problem.
The onclick function needs to increase the value of anyObj's this.width property. I originally had a line this.width += 10; in the onclick but quickly realised why this wasn't working because the this in the onclick function refers to the new button object and not the this.width property of anyObj.
The workaround I have used, and it works, is to make a copy of all the this.xxxxx properties. eg. width = this.width; and use the width variable in the onclick as you can see below. This "workaround" works fine but doesn't feel ideal to me.
So is there a better way to access the anyObj()'s properties from within the onclick function than the way I have done it? Obviously I would prefer to not have to make copies of all the anyObj() properties like I have to make them accessible to the onclick function.
Say x in a XML Http Request Object ... meaning it's either XMLHttpRequest (firefox) or ActiveXObject (IE)
This line of code works in firefox... x.someProp = "someValue"; alert(x.someProp);
But in IE I get "Object doesn't support this property or method" I need to place a custom property on the object. Is there any way I can do that in IE?
I've created an object and within this object, I've added an eventlistener. But the problem now is that after addEventListener is being called to access a callback function, the callback function is not able to access the properties within its own class. Code:
I am fairly familiar with the concept of Objects and their properties and methods, but javascript being object based as opposed to object oriented has me stumped on how to access an object's properties from an onclick event handler created for another object created within the original object.In the example below, I have a constructor function called anyObj. to which I pass an object reference to an element.
anyObj has 3 properties and one function increaseWidth()
increaseWidth() creates a new button with an onclick event handler and this is where I have a problem.The onclick function needs to increase the value of anyObj's this.width property. I originally had a line this.width += 10; in the onclick but quickly realised why this wasn't working because the this in the onclick function refers to the new button object and not the this.width property of anyObj.
The workaround I have used, and it works, is to make a copy of all the this.xxxxx properties. eg. width = this.width; and use the width variable in the onclick as you can see below. This "workaround" works fine but doesn't feel ideal to me.So, what I am asking advice on is, is there a better way to access the anyObj()'s properties from within the onclick function than the way I have done it? Obviously I would prefer to not have to make copies of all the anyObj() properties like I have to make them accessible to the onclick function.
Code:
function anyObj(divObj){ this.elem = divObj; this.width = 50;