Return Keyword Applied On Object Returns Reference Or Value?
Aug 24, 2010
I am confused about what the return keyword is actually returning when returning an object, a primitive, or a function. My confusion is compounded by the fact that I'm not sure if a function is an object or not. According to the book JavaScript Programmer Reference it is: "All functions in JavaScript are first class objects , meaning they can be passed around like any other object reference. In fact, regardless of how they are created, functions are instances of a global object named (aptly) Function."
However, someone else states that a function is not an object. An object is a set of primitives and references. A function is executable code. Functions become objects through the use of the new operator. Yet, in the book I mentioned, it says you don't need the new keyword to execute the function as an object, as it already inherits from the object Function when the function keyword is used:
function functionName([argname1 [, ...[, argnameN]]])
{
statements;
}
So there's one source of contradiction. Now the bigger issue is what is going on when the return keyword is used. Notice the Validation() function returns an object as its last expression. This technique is common, where you return an object which contains functions in form of object notation. I believe this is done so that we create a closure so that when the intepreter exits the Validation() method, since we created a closure by returning an object, which contains the inner functions addRule and getRule, the local variables of Validation() are not destroyed, given that we can reference them through the two inner functions that make use of the local variables of the outer function. So when we use the return keyword on an object literal, and then exit the function, when we call one of the inner functions as we do later:
I'm creating a list with buttons that show and hide some of the list elements to create the effect of scrolling. I've built an scrollList object so I can apply this to any list. When you create the object and pass it the id of the <ul> and how many items of the list you want to be displayed. It will add the scroll down and scroll up links and add an Event.observe to them (I'm using the Prototype library).
This code creates the object
var main_news_articles; main_news_articles = new scrollList('main_news_articles', 5); main_news_articles.init();
The problem I am having is with the callback functions of the onclick event of the links (scrollUp and scrollDown). The this keyword within these functions is refering to the link and not the object.
To try and solve this I've made the title attribute of the link the same as the variable name of the scrollList object. I can reference the scrollList object like so Code:
I know just enough javascript to be dangerous, I created a 5 star rater for pages, which is set up something like this:
Code: var starRater=function(id){ // get latest rating info from server, write into "id" div, etc } //later in code... var rating=new starRater("rating");
Over the course of development I added a comment section, page visit count and so forth, and ended up with redundant code and a lot of calls to the server. I thought instead I would make one initial call and then initialize the elements with the information returned.
To be able to call the initialize function on these elements, I need some reference to them. I could call them directly, as in the example above I could call rating.init(), but I'd have to know what variable name was given to it. Instead I was thinking I'd pass some sort of reference to the object, but that doesn't seem to be working.
[Code]...
In the code above I get undefined for raterElem.testVar. Obviously I'm missing something. What am I doing wrong?
and addClickEvent sets the onclick attribute of above mentioned DIV. and in addClickEvent I am explicitely mentioning the name of class object. This means every time I create a Variable with another name I have to Change this function also.
console.log(details.ring); // why does this return gold and not silver if req is passed by reference and we already changed its value above and so it's pointing to the same memory position?
I call the same elements tag in lots of places, I'd have to wrap a large chunk of code in eval to make it work in other browsers. Is there any other way to call a dynamic object name?
The goal is for selectedDay to be assigned the value of the system variable Mainpackage.subclass0.subclass1.firstDay and then incremented by two days. Mainpackage.subclass0.subclass1.firstDay needs to be unchanged.
Important system variable in red. To be 'manipulated and used' variable in green. Even with an intermediary, third, dummy variable it doesn't work:
Console log is:
It doesn't work in my webapp : All variables change at the same time.
If I have several links with onClick events pointing to the same function, how can I get a reference back to the link that was clicked? I'd like to change the link text after the onClick fires so the user knows something is happening, without having to create and assign id's to every link.
I have searched the web and this forum for the answer to this but am unable to find a reasonable choice. So, this is the problem I am facing.
Code: var str = "document.frames[0].document.frames[0]"; var str 1 = "document.frames[6]"; //Loop Starts { var obj1 = str + str1;
[Code]...
I know the easiest way to do this is to use eval() and it most definitely does work. The problem is that I have to use eval() inside a loop to check whether the reference is null and from what I hear, that is one of the most inefficient ways to code. I have searched for alternatives like using, var myFunc = new Function(string) and then calling myFunc(). But this does not work in my case. I cannot find any other alternative in this case. I guess one can say eval() is the only choice here but I am not too sure.
I have a function that changes the innerHTML of a DIV element. I want to pass to that function as a parameter a reference to the DIV to be changed. In the example below it works the first time (executed with the body onLoad event). As the subsequent function declaraton is rewritten by the function itself, it can no longer reference the targeted DIV. I tried to pass a string teference and use eval(), but this time it fails the third time it is executed.
Code: <html> <head> <title>Passing Parameters to a Function (with object prameter)</title> <script type="text/javascript">
For a site, I am making a listing object. What it does, is it fetches rows of data from a serve using JSON. The object has a method called loadlist.to print out the data it now does something like this:
I have just started using the data functions in jQuery for keeping track of a few items on the client. The user can click on links, which call a function that updates the display and stored data value.I am storing data as a set of columns, named c1, c2, c3 etc.
I update the relevant column like this function UpdateCol(column) {
How do I store an object reference inside a variable when I want the object reference to to reference the first "ul" html element nested inside the current object (as referenced by this keyword)?
I've been playing around with JS for a while, but I've not ever found fantastic learning resources. I'm comfortable with other programming languages, so I'm very interested in OOP programming for JS, and I've started to use tools like the Prototype library for that purpose.
However, one problem has consistently caused me problems whenever I'm writing a JS Class. How can I get a reference to the specific class instance from inside an event handler that I create with a class method. Let me give an example of what I want to do:
(This code is using the Prototype library...)
myClass = Class.create(); myClass.prototype = { makeLink: function(obj) { // Out here, "this" refers to the instance of the myClass Class obj.innerHTML = '' obj.onclick = function() { // In here, "this" refers to the clicked on element. // How do I get a reference to the instance of myClass in here? } }
Here's the best way that I know how to do this right now. It seems to work in Firefox alright, but doesn't seem to work in IE:
myClass = Class.create(); myClass.prototype = { makeLink: function(obj) { obj.innerHTML = '' self = this; // Save a reference to class instance as "self" obj.onclick = function() { // Self is evaluated to instance reference at time of assignment to onclick self.linkClicked(); }
linkClicked: function() { // Do something useful here }
}
Is there a more elegant way to do this? I could just create all of my event handlers for objects outside of the class in procedural code where I already have a reference to the class object, but I like to wrap these things up inside the class, because sometimes they get significantly more complicated than this very simple one. Anybody have any solutions? This has to be a common design pattern...
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += ' '+arguments[i++]) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; }
function f(){} show('delete(f):',delete(f)) ; // false
I'm doing something with grails + jquery. My controller send a rendered json (which I know is sending correctly) but when the object "arrives" in the callback method it comes undefined. I can see it lenght, but not it's content.
I have an object: Myobject and properties for the object: Myobject.color, Myobject.width, Myobject.height The scripts returns TRUE if I query Myobject.color or Myobject.width etc. I want to loop through de properties like so:
Code: var properties = ['color', 'width', 'height'] for (var i=0; i<properties.length; i++) { Myobject.properties[i]; // Returns UNDEFINED }
I'm new to jQuery and Javascript overall, I have searched the internet to make use of jQuery instead of the HTML's iframe tag. My problem is links inside a .load file, I want them to refresh the box on the index.php file, not the file itself (in this case the links are in blog.php)
Here's everything I can provide with: Files: "index.php" "blog.php"
In my index.php file I have this script to make my index.php links open the required file in my div box. "index.php" <script language="javascript" type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#html").load("blog.php"); // shows the blog.php when I first enter index.php, which I want to have $(".link_click").click(function(){ $("#html").load($(this).attr("id")); }); }); </script>
Here's my div box in "index.php" where I show the information within the loaded file: <div id="html"></div>
All this works, but here's my problem, I have multiple links within "blog.php" which I want to make "index.php" to refresh the "index.php"'s html div. Is there some kind of way to edit the "blog.php"'s script to make it "index.php" the parent or reference? "blog.php" <script type="text/javascript"> $(document).ready(function(){ $(".link_click").click(function(){ $("#html").load($(this).attr("id")); // Need to change this(?) to make it reference the index.php's html div box }); }); </script>