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);
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:
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.
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 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()
I have been staring at this problem for too long and most likely missing a simple solution so I hope somebody can take my blindfold off and show me the way The markup (which comes from a CMS and I cannot change it) I am working with is akin to:
Notice how the second fo 3 FAQ sections has 2 paragraphs under the 'trigger' rather than 1? Therein lies my problem, because what I am doing with prototype.js is:
1.) Loop through page and collect all <p>'s 2.) do a .each iteration and attach an onclick event on $$('p strong.open') - assume this node for each iteration is now var 'trigger' 3.) run Element.hide() on trigger.next() 4.) in the onclick event, run Element.show() for trigger.next()
- If you are still following so far - my problem is that next() only shows/hides one single element and that is the <p> holding the content - which is fine except when there are 2 or even more <p>'s with content I need to act on. I;ve experimented with nextSiblings and adjacent() and I end up with too many nodes - basically every <p> in the entire document. I just want 'hide all P's after the trigger P up until the next p > strong.open
Is it possible to access Objects like (document.form1.textfield1) with a string variable like (var obName='textfield1') and using (document.form1.obName.value)?
Ive tried it in IE but the value in the obName is not cosidered as a object name.
I need to create 14 javascript Ajax objects and assign properties and events to them. Since most of the code is just being repeated, I'd like to do the whole thing in a loop using an array for the object names. This is a portion of the code as it now stands:
boxe2_tree = new dhtmlXTreeObject("boxe2_div", "100%", "100%", 0); boxe2_tree.enableCheckBoxes(1); // etc. boxe2_tree.loadXML("./xml/boxe.xml",function(){loadTree('boxe2_tree');}); warn2_tree = new dhtmlXTreeObject("warn2_div", "100%", "100%", 0); warn2_tree.enableCheckBoxes(1);
I am trying to convert some of my javascripts into a class and am running into difficulties which i think are related to variable scope.
Basically I have a constructor function for a calendarInput class that takes 4 parameters, the first is a reference name/number for this input. I also have some functions for importing my PHP classes into Javascript using AJAX (properties only. still trying to get methods working but that's another story!). The relevant function is called call_object_method(classname, methodname, createparams, methodparams, post, callbackfunction). This creates an instance of the specified PHP class using the parameters in createparams. It then calls the specified method using the parameters in methodparams. The result is passed back to the javascript function specified in the callbackfunction parameter (ie the value of xmlhttp.onreadystatechange is set to callbackfunction before xmlhttp.send() is called)
The function i am trying to fix is called show (x,y) which creates the html for the calendarInput and displays it at co-ordinates x, y.
this.showcallback = function() { alert(this); <!--//code to create html//--> }
}
I know i've cut out most of the innards of this. This is because I have already tested these functions and had the calendarInput working outside of a class, hence am pretty sure that this is ok (plus it runs to almost 1000 lines these days!!). My problem is that when I call the show method, the alert on the first line of the callback function returns the function showcallback instead of (as i was expecting) the instance of the calendarInput object. Whilst this kinda makes sense I can't figure out how to reference the Object instead. I have tried 'this.parent' but this returned undefined. I have tried changing the way i reference the callback function (ie the final parameter of call_object_method) but no joy.
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?
According to ECMAScript, the root of the prototype chain is Object.Prototype. Each object has an internal property [[Prototype]] that could be another object or NULL.... However, it also says that every function has the Function prototype object: Function.Prototype, it confused me, because a function is an object, for a function object, what is its function prototype and object prototype..For example:
var x = function (n) {return n+1;};
what is the relationships of x, Object.Prototype and Function.Prototype
I am trying to get to the bottom of javascript object, prototypes etc. I have a fairly good grasp of it, but I get confused the closer I get to the base object.prototype. FIrst of all, I was under the impression that all objects descend directly from Object. But some objects (like Array) seem to inherit properties and methods from the function.prototype. So does this mean that the chain is like this:
object -- function -- array Second, I noticed (on the mozilla javascript reference site that object.prototype inherits properties and methods from function.prototype and vice versa!? How can this be? I must be missing something important about understanding the chain?
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?
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>
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.
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the bind function.
The problem with Javascript is that the "this" operator is poorly overloaded and it is often hard to understand in the context of object-oriented javascript
As I read this, it states that all functions (which are themselves objects) will, in the future, have an associated method called "bind". The function() function, so to speak, simply instantiates a Function object with the parameter list and then evals the statement, sticking the resulting execution-tree in the current code frame.
The "this" there refers to the function object associated with the call to bind(), right? But the word "arguments" there refers to the arguments passed to the function object *generated by* the call to bind().
In every example within prototype.js, bind() is called either in a constructor or within a method contexted to a javascript object, and is always called with "this" as its argument, e.g.:
As I read the code it seems to be stating that the "this" object referred to within bind()'d functions are being coerced into always referring to the current instantiated object.
But isn't this always the case anyway? Is this a rather confusing attempt to ensure "this" purity whereby the call
method.apply(object, arguments)
is forced to always have the reference to the containing object present?
I think I've got it. Bind() generates uniq functions that contain live references to the objects to which they belong, such that the function object can then be passed to setTimeout() or onMouseOver(), handlers that accept functions but not objects.
Is there anyway to 'restore' the original alert() method or is it gone forever?
I know you can do window.alert = Window.prototype.alert, but lets say you also set Window.prototype.alert = function() { } or lets say we're in Opera, which doesnt have a Window "class".
I wanted to add an object as a prototype to separate my methods more nicely, however, I ran into a couple of problems. Apart from the obvious "scope" issues I found that any instances of my class shared the objects methods and properties.
I realise (now) that this is actually how prototypes work, they share functions and objects rather than create new instances of them for every "class", but is there any way around it? (or shouldn't I be doing things like this at all?) Code:
I am working on my own pop up calendar, mainly because the one I am currently using crashes the Safari browser at times.
So, I want to verify that what I am doing will work, in that I want to be able to have multiple calendars open at the same time, each independent of the other.
So, I start it off with:
var Calendar = { dateSelected: null, topPos:null, leftPos:null,
somefunction:function(e) { ... } };
If I create more than one calendar object, will they have their own variables, in that the dateSelected, topPos and leftPos will be unique to that instance?
Or, is there a better way to do this, that is cross-platform.....
I want ask you if, for a web portal/application, is better prototype or Jquery? I don't want to innesc some type of flame, but after the announce that drupal use JQuery and that the new Wordpress 2.2 use Jquery I ask myself if my choice of use prototype.js is the bettere choice.