Object's Prototype Variable Inheritance

Aug 29, 2006

I'm looking to do something like this:

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);

View 1 Replies


ADVERTISEMENT

Prototype Inheritance

Mar 4, 2007

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:

View 2 Replies View Related

Inheritance : Access To The SuperClass' Methods Via Prototype

Sep 20, 2005

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.

Here is the code, if any of you have got ideas...

View 2 Replies View Related

JQuery :: Classes - Objects And Inheritance ?

May 28, 2010

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?

View 2 Replies View Related

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()

[Code]...

View 11 Replies View Related

Prototype.js Hide/show With Variable Html Elements?

May 27, 2009

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:

<div id="faq">
<p><strong class="open">FAQ 1</strong></p>
<p>FAQ 1 content lorem ipsem lorem ipsem ad infinitum</p>

[code]....

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

View 3 Replies View Related

Acces Objects Using String Variable: Is It Possible?

Jul 20, 2005

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.

View 2 Replies View Related

Create Objects With Variable Names?

Jun 14, 2011

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);

[Code]...

View 3 Replies View Related

Objects, Callback Functions And Variable Scope

Nov 23, 2005

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.

So, my code is as follows:

function calendarInput(calendarid, target, displaytarget, value)
{
this.calendarid = calendarid;
this.target = target;
this.displaytarget = displaytarget;
this.value = value;

this.show = function(x, y)
{
<!--// bunch of init and prepping code. includes creating
createparams and methodparams arrays //-->

call_object_method("cms_calendar", "getcalendarview",
createparams, methodparams, false, this.showcallback);

}

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.

View 2 Replies View Related

Inheritance Question

Mar 3, 2005

Suppose I have:

Code:
function A() { ... }
function B() { ... }
What's the difference between:

Code:
B.prototype = A
and

Code:
B.prototype = new A
?

View 5 Replies View Related

Inheritance Issues

Aug 17, 2006

Let's consider this example:

function Polygon(iSides) {
this.sides = iSides;
}

Polygon.prototype.getArea = function () {
return 0;
};


function Triangle(iBase, iHeight) {
Polygon.call(this, 3);
this.base = iBase;
this.height = iHeight;
}

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?

View 2 Replies View Related

Difference Between Object.prototype And Function.prototype?

Nov 25, 2011

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

View 5 Replies View Related

How Come Object.prototype Inherits From Function.prototype

Dec 14, 2009

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?

View 24 Replies View Related

Classes Inheritance Problem

Jul 23, 2005

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.

View 2 Replies View Related

Noodle With Prototypal Inheritance?

Jul 30, 2010

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?

View 1 Replies View Related

Benefits Of Prototypal Inheritance Over Classical?

Dec 29, 2010

What are the benefits of prototypal inheritance over classical inheritance?

View 1 Replies View Related

Failure Of Inheritance From A Style Statement?

Oct 20, 2009

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>

[Code]....

View 2 Replies View Related

Objects Under Mouse - Return An Array Of All Objects Underneath A Certain Point

Apr 17, 2011

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.

View 1 Replies View Related

JQuery :: Extending Objects With Internal Objects?

Sep 5, 2009

Is there a better way to extend object with internal objects?

$.fn.bestShow = function(s) {
var d = {
width: 0,
height: 0,
order: "numeric",
orderBy: "",

[Code]...

View 3 Replies View Related

With And Prototype

Aug 24, 2005

I can use "with" like this:

function MyObject(message)
{
this.message = message;
}
function _MyObject_speak()
{
alert(this.message);
}

with (MyObject)
{
prototype.speak = _MyObject_speak;
}


I was wondering why I can't use "with" like this:

with (MyObject.prototype)
{
speak = _MyObject_speak;
}

View 8 Replies View Related

Cracking Prototype.js

Jul 23, 2005

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

So, let's start with the definition:

Function.prototype.bind = function(object) {
var method = this;
return function() {
method.apply(object, arguments);
}
}

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.:

this.options.onComplete = this.updateContent.bind(this);

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.

View 8 Replies View Related

Object.prototype

Mar 15, 2006

Lets say we run: window.alert = function() { };

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".

View 3 Replies View Related

Prototype.js BindAsEventListener Without 'this'

Jun 5, 2006

I would like to set up an event observer outside of an object, so I
can't use this.bindAsEventListener. How can I pass the correct object
reference?

I tried something like this, and various other variations, but no luck.
This works when I set it up from inside the object, using "this.",

Event.observe(targetId,'click',targetId.select.bin dAsEventListener(this),false);

View 28 Replies View Related

Object As A Prototype.

Jan 11, 2007

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:

View 2 Replies View Related

Having More Than One Object Without Using Prototype

Jan 23, 2007

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.....

View 8 Replies View Related

JQuery Or Prototype ?

May 16, 2007

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.

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved