Object Members That Are Prototyped As Arrays Become Shared By All Class Instances?
Dec 12, 2010
Has anyone noticed this behavior before? This really threw me off... I would have expected prototyped arrays to be private to each class instance rather than shared between all class instances.
HTML Code:
<html>
<head>
<script type="text/javascript">
It is going to be a clone of the game ChuChu Rocket! that I used to play on the Dreamcast.
The game is set up so that the levels sit inside a nested array of objects, like your typical JSON string. So that within levels[] there are objects with level names, an array of moveable items, an array of arrows you can place, etc.
I have set it up so that when the level is loaded in to the DOM, it also duplicates a part of the object, the part responsible for the positioning of the moveable items on screen so that when they are animated, and if there is a fail scenario (cat eats mouse, cat gets to rocket before mouse) you should be able to reset and it remembers your coordinates.
From what I have learned in my years of studying and using JavaScript, the following should always hold true: a=1; b=a; b++; // b would become 2, a would stay as 1 (they are separate variables)
I seem to have found an exception to this rule.
When creating a level, I duplicate the part of my object called o.chuchu and I call it o.chuchuMoving
If I make a change to anything in o.chuchuMoving, it also changes o.chuchu.
o.chuchu and o.chuchuMoving are arrays within the object. Formatted like this: [0,0,1],[1,0,1],[2,0,1],[3,0,1] ... etc
If I tell my object the following: o.chuchuMoving[0][0]=1... then o.chuchu[0][0] also becomes 1.
Please check the code at [url] when you get a moment
I'm not sure why, but the Console.focus() and Console.writeln() methods just don't seem to be able to use the DOM references stored in Console.STDIN and Console.STDOUT. Everything's fine in the constructor, but other methods can't seem to use them.
I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts.
I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and protected. By protected this means that they can be overridden (or reassigned) in subclasses, and privileged subclass methods can access them, but callers external to the class cannot.
I've been attempting to define a factory function that transfers its parameters to an anonymous object while defining the constructor in a -with- block. (This is what I'm loosely referring to as "subclassing" - it's really object composition.) This works, but not for externally-defined functions. They can't see the protected static members....
I have two result. SelectableChildren: groupHead.nextAll(".SimpleButton") and SelectableChildren: groupHead.nextAll(".SimpleButton").next().children(".SimpleButton")
I would like to combine them into one object so that I can bind a handle to them in one loop. $.each(combineResult, function(index, object){ ... })
I have to make them in one, so that I can pass it around.
LiveSearch.prototype.initialize = function() { $("#thebutton").mousedown(function() { //when we click the button alert(this.theglobal); }); }
$(document).ready(function() { var objSearch = new LiveSearch("globalvalue"); }); </script>
On page load I create a new LiveSearch instance and it assigns theGlobal = "globalvalue" and proceeds to initialize(); At this point Im using JQuery to setup an onmousedown event on a button on my page with id="thebutton". When I click the button the alert comes back with 'undefined'. How can I get direct access to my theglobal variable? Code:
Trying to figure out OO programming for JavaScript and I'm totally lost.
Here's what I'm trying to do:
I'm posting search data to a PHP form to run a query on a database.
The structure is sort of like this:
So I'm thinking I need to create search objects or arrays. Initially, I thought I could do something like this:
Code:
This doesn't seem to work. It says object not defined whenever I try to do something.
I've also tried creating a JSON object like this:
Code:
This will allow me retrieve the data like I want to:
Code:
The problem is I can't figure out how to add data to the JSON object without manually typing it in. I need to be able to loop through form elements on my HTML page and set them to these variables. I'm using jQuery and doing something like this:
how to search through siblings for shared attributes I have a a long list of links and I suppose Im going to apply a number of classes to each one [describing a number of attributes for each] and I want to do a conditional search probably in javascript/jquery so that when i hover over one of the links I highlight its relatives - those that have the same attributes.
What Im having trouble wrapping my head around is the idea of classes to ID these custom attributes. The attributes are coming from a database converted to XML. In my Javascript I essentially want to say that
for each link should the user mouseover or mouseclick for each of its sibling links if its sibling link share its attributes style or highlight these compliant siblings a certain way
how do I do the 'if statement' without spelling out every possible attribute entry? I know in plain OOP Java how I could test for this but in that scenario I have access to these entries as fields in an object's instantiation.
A remotely shared white board, using regular JavaScript,jQuery, the canvas element and PHP.I (or someone else) can get this thing working.The new 8:25 a.m. (US CST) version in http:[url].... should work -- though I still have not seen it work, perhaps.
To have multiple linked sliders that all share a maximum value, for example, I would have 10 sliders, each set to 50, with a pool of 10 additional points to spend. If I assign all 10 points to 1 slider, the other 9 are locked out, or I could spend 1 point on each slider.
Is there anyway to have a CSS class returned as an object? This would be rather useful to feed, for example, to the .animate() function. There is no way to animate directly to a CSS class as destination, is there?
I have a javascript object which dynamically generates a table adding, deleting and moving rows as the user clicks on buttons or links. Problem is when I generate a table row and add the javascript method call to my class, I have to put the object instance name variable of the class in order for it to be called from the onclick=function(). This is seriously limiting, but I'm stuck for a way round it. Heres a edited of the code so you get the idea....
Instantiating the object :-
var dtl = new DynamicTableList("table1", $ {myObject.allFieldsAsJavaScriptArray}, true, true, true);
My javascript class DynamicTableList, note the dtl javascript object instance variable being referred to in the addRow function. How can I avoid this???
if (showDelete) { var cell2 = document.createElement('TD'); var inp2 = document.createElement('IMG'); / ************************************************** ***********************************************/ inp2.onclick=function(){dtl.delRow(this);} // Have to specify dtc!!!!!!!! / ************************************************** ***********************************************/ inp2.title='Delete' inp2.alt='Delete' inp2.src='images/delete.gif' cell2.appendChild(inp2); row.appendChild(cell2); }
...
tbody.appendChild(row);
this.processRows(); };
this.moveRow = function(node, vector) { }; }
Obviously the code dtl.delRow(this); is being dynamically generated, but how do I replace the dtl instance name with something that'll work whatever the user of this class calls the instance of it!
I have an each loop that goes through all the elements in a certain class. What I need to do is get the ID of the current element in the each loop, but I can't seem to figure out how to. var currentID =$(this).attr ("id"); was my first assumption, but it's not working. [code]The css is just for debugging, not the goal, so alternate ways for me to do that won't help.
I am working on a Master-Detail grid that allows users to click on master rows to show or hide details rows. [code]What I want to do is to add Expandable class to SummaryRow if it's collapsed, add Collapsible class if it's expanded. But I don't know how to do that.
I've been writing a slideshow script that I've had working perfectly on my mac in Chrome and Safari, however I went to look at it in IE8 and lo and behold it falls over. It says 'object doesn't support this property or method' in relation to the new SlideShow line in the main() function.
I have a form that is done in php I'm converting it to .NET but the java script has an error. I posted all the code but the error is in the "saveOtherSurvivorValues" function i think. What happens is when you click the "add survivor" link to add a the first survivor and then fill out the information for the first survivor then click add survivor a second time for a second survivor it moves the data from the 1st survivor into different fields to produce in accurate data.
Here is a link to the live form.
[URL]
using System; using System.Collections; using System.Configuration; using System.Data;
I have a form that is done in php I'm converting it to .NET but the java script has an error. I posted all the code but the error is in the "saveOtherSurvivorValues" function i think. What happens is when you click the "add survivor" link to add a the first survivor and then fill out the information for the first survivor then click add survivor a second time for a second survivor it moves the data from the 1st survivor into different fields to produce in accurate data.
using System; using System.Collections; using System.Configuration;
The problems with the setTimeout and setInterval functions provided in Javascript are twofold. First, you can't call a local object method without losing your scope, and second, you can't pass objects to the function, since the function call is implemented as a string.
The Timer class solves these difficulties by employing a static array to store the parent object and function arguments until the function is called.
This class is provided as-is and pro bono, so go ahead and muck with it if you see things that could be done better.
Thanks to WA for giving me the idea for this (albeit indirectly)!
Updated 4/18/2003: Footprint decreased, minor code improvements. Updated 5/3/2003: Minor comment clarification; no code changes. Updated 5/10/2003: Minor code improvements. // The constructor should be called with // the parent object (optional, defaults to window).
function Timer(){ this.obj = (arguments.length)?arguments[0]:window; return this; }
// The set functions should be called with: // - The name of the object method (as a string) (required) // - The millisecond delay (required) // - Any number of extra arguments, which will all be // passed to the method when it is evaluated.
Timer.prototype.setInterval = function(func, msec){ var i = Timer.getNew(); var t = Timer.buildCall(this.obj, i, arguments); Timer.set[i].timer = window.setInterval(t,msec); return i; } Timer.prototype.setTimeout = function(func, msec){ var i = Timer.getNew(); Timer.buildCall(this.obj, i, arguments); Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec); return i; }
// The clear functions should be called with // the return value from the equivalent set function.
I have created a class array (or variable that contains an array) and am trying to add an element to the array when a new Element object is created. Thus, the line of code to add a new element to the array is within the Element object's constructor function. However, the browser just gives me an error "object expected on line 9", the line of code trying to add an element to the array.
Are arrays not functional within objects? Or, is it that the array is made as a class variable?
Code: function Element() { // INSTANCE VARIABLES this.name = "newElement" + Element.total_elements_created; // GIVE UNIQUE NAME // INITIALIZATIONS Element.element_count ++; // INCREASE TOTAL NUMBER OF ELEMENTS EXISTING