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


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

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

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

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

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

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







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