Resolved Inheritance And Nested Objects (non-DOM Related)?
Nov 17, 2009
I posted this once, but it disappeared, and I have no notifications that I did anything wrong. I read the rules before posting and wasn't breaking any so I am not sure why it disappeared but here goes again.
I am trying to learn Javascript (particularly OOP) from a series of screencasts by Douglas Crockford. I have developed a theoretical "game" to build to illustrate it to myself better, and learn by example.
I must be misunderstanding how inheritance works, because my code is not producing the results I thought it would. Here is what I have, followed by an explanation of my understanding. $(function()
Basically i have a good experience and knowledge with prototype.js library and also at the same time i am willing to get deeper into jquery.
I very much use oops concept now a days in almost every problem i solve using javascript, and was doing with prototype's feature to create classes and objects. Since i am now looking to get deeper into jquery, i was looking to find similar feature in jquery as well. I suppose jquery must be having a feature to create classes and do inheritance programming, but i couldn't find the way. My question to the forum is, Is there any option/feature in jquery which helps us to create classes and objects of our own probably with the support of inheritance?
I am having trouble with a recursive function I created to list out the indexes and values of various nested objects. var macros={ CTRL: { ALT: { ENTER: "<br />", P: "<p>.</p>", A: "<a href="">.</a>", _1: "<h1>.</h1>", _2: "<h2>.</h2>", _3: "<h3>.</h3>", _4: "<h4>.</h4>", _5: "<h5>.</h5>", _6: "<h6>.</h6>" }}};
I want to print out the whole 'bread crumb trail' to each object, ie CTRL ALT 6: <h6>.</h6>, but at the moment my function prints out CTRL ALT twice for 'ENTER' and then never again. The function: function printObj(obj) { for(i in obj) { var n=i.replace("_", ""); document.write(n); if(typeof obj[i]=="string") { document.write(":"); document.write(" "+obj[i].replace(/</g, "<").replace(/>/g, ">")); } else { document.write(n); } if(typeof obj[i]=="object") printObj(obj[i]); document.write(" "); }} printObj(macros);
I'm writing examples for placing standards-compliant java applets in XHTML or HTML documents. And I'm also testing with Javascript. At some point, I created two 'object' elements, each with their 'param' children. Code:
I'm trying to reuse some code in a different context to do a different job.The code to be reused contains hundreds of lines similar to a = new b.c.d(e,f) with different value for e and f.I need to create a new user defined object with the structure b.c.d. I've made numerous attempts along the lines of:
function d (e, f) { this.e = e; this.f = f; }
[code]....
with various permuations of functional declarations.However I get error message "Object expected" or "b.c.d is null or not an object" at the final line of the example.It works with the test line var a = new d("test", "message") but not when I start to build up the expression.How should I define of b.c.d?
I have the following object: var wDefaults = { "skin":1, "pagewidth":"960px", "c1col":[ {"wid":"w001","pcol":0,"wpos":0,"wclosed":"false","wcollapsed":"true"} ], "c3col":[ {"wid":"w002","pcol":0,"wpos":0,"wclosed":"false","wcollapsed":"false"}, {"wid":"w003","pcol":0,"wpos":1,"wclosed":"false","wcollapsed":"false"}, {"wid":"w004","pcol":0,"wpos":2,"wclosed":"false","wcollapsed":"false"}, {"wid":"w005","pcol":1,"wpos":0,"wclosed":"false","wcollapsed":"false"}, {"wid":"w006","pcol":1,"wpos":1,"wclosed":"false","wcollapsed":"false"}, {"wid":"w007","pcol":1,"wpos":2,"wclosed":"false","wcollapsed":"false"}, {"wid":"w008","pcol":1,"wpos":3,"wclosed":"false","wcollapsed":"false"}, {"wid":"w009","pcol":2,"wpos":0,"wclosed":"false","wcollapsed":"false"}, {"wid":"w010","pcol":2,"wpos":1,"wclosed":"false","wcollapsed":"false"}, {"wid":"w011","pcol":2,"wpos":2,"wclosed":"false","wcollapsed":"false"} ]}
First, how do I access a single value? I've tried some of the examples I saw on the forum, but nothing happened. The alert did not fire: alert(wDefaults.c3col[0].wid); //first try alert(wDefaults.c3col.0.wid); //second try var obj = wDefaults.c3col[0]; //third try alert(obj.wid);
The nested objects are the properties for widgets within a portal. After the user moves around the widgets, I need to update the object, putting the widgets in a different order within c3col and updating the values saved in pcol, wpos, wclosed, & wcollapsed. I also may need to add or delete an object, for example, delete wid=w003 and add a new one called w012.
My logic would be: for each widget on the page found = false find the object in c3col where wid == x and update its other values . set found = true if found == false then add a new object to c3col with x & its properties end for for each object in c3col if that wid isn't found in the list of widgets on the page, delete it from c3col end for sort the objects under c3col by pcol and then wpos
The red parts are the ones I'm unclear about how to do it. My basic questions are: (1) How do you access a single nested object? (an object within c3col, find and change its values) (2) How do you add a new nested object to an existing object? (add a new object to c3col) (3) How do you delete an nested object? (remove an object from c3col) (4) How do you sort nested objects? (sort the objects in c3col by pcol and wpos)
In my research in the javascript language I have encountered problems with implementing prototype inheritance while preserving private methods functioning properly. Here is an example: Code:
Triangle.prototype = new Polygon(); Triangle.prototype.getArea = function () { return 0.5 * this.base * this.height; };
Here are my questions:
1/ "Triangle.prototype = new Polygon();" : Doesn't this line overwrite all the properties already defined in the Triangle function?
2/ "Triangle.prototype = new Polygon();" : Won't this same line assign this.sides an empty string (or undefined ??) if one considers that the polygon function has no arguments? Or does writing ...prototype = new SomeClass() only "touch on" the methods?
I've discovered the following problem while building an APP:
/* Code Start **************************/ // Definition function cClass_prototype_prototype() { this.array = new Array; this.className = "cClass_prototype_prototype"; }
function cClass_prototype() { this.className = "cClass_prototype"; }
cClass_prototype.prototype = new cClass_prototype_prototype;
function cClass1() { this.className = "cClass1"; }
function cClass2() { this.className = "cClass2"; }
cClass1.prototype = new cClass_prototype; cClass2.prototype = new cClass_prototype;
oClass1 = new cClass1(); oClass2 = new cClass2();
// Testing
alert(oClass1.array) alert(oClass2.array)
oClass1.array.push(1);
alert(oClass1.array) alert(oClass2.array)
/* Code End ****************************/
If you will execute this code you will see that pushing an value into the first class instance array property cause changing value of the second class instance array property.
Is it possible to have array values not connected to each other? I would like to avoid defining "array" property in cClass1 and cClass2 (in this case it would work the proper way) and have them defined only in third-level parent class cClass_prototype_prototype.
please bear with my noobishness, but i've been trying for many hours to understand what is going on behind this code:
[Code]...
actually construct an instance of Employee if the reference to its constructor has been replaced by an instance of Person that only contains name and age properties? how is Ken ever initialized by Employee constructor?
function superDuperObject(range) { this.startContainer = new superDuperObjectStartContainer(range); }
function superDuperObjectStartContainer(range) { this.calculatedContainerNumber = doSomethingAndGetSomethingBack(range); return range.startContainer }
function doSomethingAndGetSomethingBack(range) { return someCoolInformationThatICalculateInThisFunctionTha tNeedsTheRange; }
var myObject = new SuperDuperObject(DOMRangeObjectThatIDefindedEarlie rInCode);
// Show me the startContainer of "DOMRangeObjectThatIDefindedEarlierInCode" alert(myObject.startContainer);
// Show me the calculatedContainerNumber that I get from a function that does stuff with the range passed to it alert(myObject.startContainer.calculatedContainerN umber);
The first div in this example has the position property stated in the div itself. The second has it stated in a separate style statement, and it doesn't get inherited, though another property from the same style statement DOES.
Firebug makes no complaints.
Is there a better way to refer to the jquery library files, so that you don't have to fix my pointers to my local library?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head>
What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass' priviledged methods also. Do you know if this is possible ?
In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method of ObjectC refers to the 'toString ()' method of ObjectA. This is possible because the 'Object.inherits ( superClass )' method adds a reference to the superClass of an object in the object's prototype.
If I understood things correctly, the prototype is the same for all objects of class ObjectC. Therefore, I should be able to only add the reference to the superClass once. That is the purpose of the 'Object.initializedPrototypes' array : it keeps track of the objects for which a reference to the superClass has been added to the prototype.
However, when I create another instance of ObjectC (variable c2), its prototype doesn't contain any reference to the superClass.
A workaround for this problem consists in adding a reference to the superClass for each instance of the inferiting object (either by bypassing the check to Object.initializedPrototypes or by adding the reference to a priviledged member such as 'this.superClass' instead of 'this.prototype.superClass'). However, in terms of memory usage or of "programming elegance", this seams to defeat the whole purpose of using prototypes.
Is there a way in Javascript or Jquery to return an array of all objects underneath a certain point, ie. the mouse position. Basically, I have a series of images which link to various web pages but I have a large semi transparent image positioned over the top of the other images. I want to find the href of the background image that the mouse pointer clicks over.
Im not sure my subject decsription gives an exact description of what i am after but i will try and explain my requirements a bit better.I have an header menu there are items in this menu but there are a lot of them and if you try view on a normal 1024 x 760 you have to horizontal scroll to view all the items, but on my monitor 1920 x 1080 it is fine. I don't want to do it this way since I hate having to scoll both horizontal and vertical. I would like to be able to change the items on this header to become a rollover dropdown menu when they would normally be hidden becuase of the screen width. I have seen this done before I am just unsure about how to implement this.
[URL]. In the picture I have a picture div and a text div. The picture and text are related to one another. I want it that when you click the right or left arrow the pic and text change together. I dont really care how they change, slide, fade, or simple do a show/hide kind of effect. How to make one div have a slide show when you mouseover to right or left it moves.
My application displays a list of checkboxes to the user. Some of the boxes are related. For eg:
List of boxes: a b c d
Checkboxes a and c are related. If user checks a; c should automatically be checked and vice-versa. Same for de-checking. I need a function to do this.
I'm not sure whether this is a js or css issue, as it's css and js mixed thats causing an issue. ( I have added this to the css forum as well, but I haven't received any thing that really helped ).
when a user scrolls to the bottom of my page. i found .scroll which just tells me when there is a change to the scroll bar position.
i want to hide widget on pageload then have it pop-up when bottom of article is reached. and if the person scrolls back upm then it will dissapear again.
This is related to the pop-up window. Below is the function related to pop-up window. function openPopUpDetailsMultiple(seqNum ,controllername ) { if (!popUpDetails||popUpDetails.closed) { popUpDetails=window.open("GetSCEventAddData?schandle=<!.schandle>&objID=<!.objID>&seqnum="+seqNum+"&controllername="+controllername+,"popUpDetails","toolbar=1,location=0,scrollbars=1,width=600,height=300,resizable=1,left=200,top=200"); }else{ popUpDetails.focus(); popUpDetails.location="GetSCEventAddData?schandle=<!.schandle>&objID=<!.objID>&seqnum=" + seqNum; } }
The problem I'm facing now is in this piece of code: ><a href=javascript:openPopUpDetailsMultiple('"+ scEv[i].sceSeq+"''"+ scEv[i].sceCntr +"')><img src=images/NsaMoreDetails.gif border=0 height=16 width=16 align=middle alt='View more details'>More details</a></td></tr></table></td></tr></table>"); I do not know how to send these (scEv[i].sceSeq,scEv[i].sceCntr) arguments. What might be the delimiter for these arguments.
I want to load data after loading related php page. i used ajax and even jquery.
jquery was not sure this is jquery code:
But this and when i was using ajax it changes data, but unfortunately, the pay now button (pay pal) was not working. curser changes but button don't work...
I have a requirement in which, I need to capture a loan amount and the amount of down payment for that loan. According to the requirement, the user is going to enter enter the loan amount and then will enter the down payment in either percentage of the loan amount or an actual amount. I have designed a form to capture this information with three text fields, one each for loan amount, down payment in % and down payment in $.
So the user has the option of entering downpayment in $ or in %. If the user enter the down payment in $ then my code has to calculate the equivalent % value and populate the corresponding text field and vice versa. My question here is, what would be the best way to handle this situation.
Shall i just use the OnChange event of the text field to handle this scenario? If I do that wont I be running into a loop?