accessing methods of base class

Referring to example:

function One() {
    this.toString = function() {return 'One';};
}

function Two() {
    One.apply(this);
    this.toString = function() {return <access One class toString> + '\n' +
'Two';}
}
Two.prototype = new One();


Is it somehow possible to access the original toString method of the base
class once it has been overridden? So that the overriding version can
'append' to the original? (see example).

Also, doesn't the Two.prototype = new One(); statement result in the One
constructor being executed twice?

Thank you.


0
mike
7/17/2003 4:27:07 PM
netscape.mozilla.jseng 1530 articles. 0 followers. Follow

3 Replies
342 Views

Similar Articles

[PageSpeed] 40

mike caines wrote:

> Referring to example:
> 
> function One() {
>     this.toString = function() {return 'One';};
> }

It's better to share methods via the prototype, so:

function One() {}
One.prototype.toString = function () {return 'One';};

> 
> function Two() {
>     One.apply(this);
>     this.toString = function() {return <access One class toString> + '\n' +
> 'Two';}
> }
> Two.prototype = new One();

Likewise:

function Two() { /* you could call One.apply(this); here */ }
Two.prototype = new One;
Two.prototype.toString = function () {
   return One.prototype.toString() + '\nTwo';
}

> Is it somehow possible to access the original toString method of the base
> class once it has been overridden? So that the overriding version can
> 'append' to the original? (see example).

There are several ways; I've shown one above.  You could also give the 
base toString method a name in the global object, by declaring it as a 
top-level function (instead of writing a function expression).  Or in 
SpiderMonkey and Rhino, you could say this.__proto__.toString() in Two's 
toString body.

> Also, doesn't the Two.prototype = new One(); statement result in the One
> constructor being executed twice?

No.  Why would 'new One()' call the One constructor twice?

/be

0
Brendan
7/20/2003 6:45:40 PM
Michael Caines wrote:

> I was mistaken, the output of the window.status statements led me to believe
> this was the case. The Rectangle.prototype = new Shape statement does run the
> Shape constructor though, does it not? I found this strange since at this point
> one is still 'defining classes'.

There is no such thing as a class in JS, and no phase of compilation or 
execution as 'defining classes'.

Everything is a statement; declarations are preprocessed according to 
certain rules, but assignment statements run in order and according to 
the usual control flow rules.

> Also, why does a call to the inherited getType method of a sub class (in this
> case Rectangle or ThreeDRectangle) return 'Shape' (ie. the value from the base
> class), even though the type var has been redefined? Is it possible to override
> instance/private var's in the sub classes? As it is now it seems all three
> 'versions' of the var exist separately.

Your code says:

function Shape(Number_x, Number_y) {

	var type = 'Shape';
	var x = 0;
	var y = 0;
         . . .
}

Those variables are simply function locals, they are not instance or 
member variables of the objects created via 'new Shape(...)' 
expressions.  If you want to set properties of the newly created object, 
assign 'this.type = "Shape"; this.x = ...'.

This is all well-covered by David Flanagan's O'Reilly book, "JavaScript: 
The Definitive Guide."

/be

0
Brendan
7/21/2003 2:24:21 AM
"Brendan Eich" <brendan@meer.net> wrote in message
news:bffi8e$lan1@ripley.netscape.com...
<snip>
> Your code says:
>
> function Shape(Number_x, Number_y) {
>
> var type = 'Shape';
> var x = 0;
> var y = 0;
>          . . .
> }
>
>Those variables are simply function locals, they are not
>instance or member variables of the objects created via
>'new Shape(...)' expressions. ...
<snip>

There are circumstances were the local variables (including inner
function definitions) of a constructor function may be considered
private instance members:-

<URL: http://www.crockford.com/javascript/private.html >

Though using the technique Douglas Crockford describes on that page
renders normal prototype based inheritance impractical.

Richard.


0
Richard
7/21/2003 9:27:45 AM
Reply:

Similar Artilces:

access derived class propertity in base class
Hello all, It is possible to access derived class's propertity from within in base class? for example public class DerivedClass : BaseClass { public int myID; private void myMethod() { this.BoundData(): } } public class BaseClass { protected virtual void BoundData(); { // It is possible to access derived class's propertity myID // from within in base class? myID = 102; } } your help is appreciate! Peter As I understand, the only way to access the properti...

access pages base class from external class
our web pages inherit frm a Pagebase class that contains some functionality to provide translated test. I would like to make use of that functionality in a class that gets instantiated by the page. how can that class get a reference to the Page class ?Mike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/ http://www.codeproject.com/dotnet/CheatSheetCastingNET.asp casting is not a problem, but i need something to cast first. from a usercontrol, i can get to my pages pagebase using me.parent.page but what...

Class that inherits from base class is calling method twice
I am using a base class the manages categories. It is marked MustInherit but as a base class it mostly encapsulated the methods and business logic of any class that inherits it. This way, I can use an ObjectDataSource and set the type name to "NewCategories", which is a class that inherits from CategoryManager The methods in the CategoryManger are Protected and Overridable The methods in the NewsCategories class are Public Overloads I am having an issue where a method is being called twice.  I have three list boxes, each with their own datasource, but use the same type name (Please ...

Calling base class static method using derived class
Hello, In C++, I can do this (ignoring constructors, etc.): class Base { public: static int baseMethod (void); }; class Derived : public Base { }; Base::baseMethod (); Derived::baseMethod (); i.e. I can use the derived class type to call the base class static method. Should I be able to do this in javascript/SpiderMonkey? I did the equivalent to this, then wrote this JS code: var b = new Base (); b.baseMethod(); Base.baseMethod(); var d = new Derived (); d.baseMethod(); Derived.baseMethod(); The JS engine threw an exception at the last line (Derive...

Method for access a class
Hi,im new to ASP.NET and try to create a simple helpdesk for my colleagues.So, i create a DB in SQL2005 with one table "Problem". Then i create an aspx-site with textboxes. Next i create a stored procedure: 1 ALTER PROCEDURE dbo.sp_ProblemHinzufuegen2 @TicketID int,3 @Rechnername nchar(30),4 @Standort nchar(50),5 @Ansprechpartner nchar(30),6 @Telefonnummer nchar(20),7 @EMail nchar(35),8 @Beschreibung nchar(2000)9 10 AS11 INSERT INTO Problem(TicketID, Rechnername, Standort, Ansprechpartner, Telefonnummer, EMail, Beschreibung)12 VALUES(@Ti...

How to override a function of the base class of base class [Edit]
The codes are: {code} TGrandFather=class procedure Fun();virtual; end; TFather=class(TGrandFather) procedure Fun();override; end; TSon=class(TFather) procedure Fun();override; end; {code} When the TGrandFather.Fun() is called, the TFather.Fun() is called instead, but not the TSon.Fun(). How to make TSon override the TFather.Fun()? Persanker wrote: > The codes are: You cannot declare a function without a return type. > When the TGrandFather.Fun() is called, the TFather.Fun() is called > instead, but not the TSon.Fun(). That means a TFather object was i...

Access class from other class
I'm just beggining to get into ASP.NET and doing well so far. I'm so used to ASP's "spaghetti code" that I find this OOP sometimes daunting :) The Q: How is it that I can access classes, or methods or procedures, from other classes. For example, I have a user control (UC) that has a code-behind that accesses a cookie and returns a value from the cookie. If I have another UC do I have to rewrite the same procedure or is there a way to access this already written procedure in the other UC? I apologize for any incorrcet terminolgy 8] ...http://developer.webchanix.com ...

accessing a method from another class
hi everyone! i am working with Web Matrix and i have 2 classes and would like to access the method of one class from inside the other one and I get the following error message: CS0246: The type or namespace name 'CodeBehindFinder' could not be found (are you missing a using directive or an assembly reference?) my page directive for one aspx is: <%@ Page Language="C#" Inherits="CodeBehindFinder" Src="venue_finder.cs" Debug="true" %> and the class structure is: using System; ..... public class CodeBehindFinder : Page...

Generics: how to create a base generic class to use on other base class
Hi, I am stuck on a Generics class that must be used in another base class and interfaces that the descendant actually supplies the actual type. I cannot get this to work and my google search have not lead me anywhere... Here are the breakdown of classes.. TBizObj = class TBizList<BOType: TBizObj> = class... property Items[index: integer]: BOType ... end; With generics, TBizList<> works out great...before it, I had TBizList for each TBizObj class.. However, I have other base classes and interfaces that uses TBizList<> and I don't know how to creat...

How do you access the Profile from a base class?
Hi All,All my aspx pages inherit from a master base class. I have done this so that I can set things that apply to every page in one place. Problem is that I cannot access the profile from the base class. I assume I just need to import something but I dont know what. Maybe there is another way to access the profile information. Any ideas welcome!DannyNote to moderator: This post relates to http://forums.asp.net/thread/1393643.aspx however, I think it is in the wrong category and I didnt fully understand the problem. Hence the need for a new clearer post. (if you get what I mean) I sup...

Unable to access method of class
--0-333880187-1103071699=:23842 Content-Type: text/plain; charset=us-ascii I am using Rhino to access methods and objects of java classes. I am trying to access a method of class as follows: function test1(p1, p2, p3){ e = new xyzException(); try{ printStr("Calling API "); abcUtil.abcImport(p1, p2, p3); } catch (e) { printStr("RESULT=FAILURE"); printStr(e.toString()); printStr(e.getMessage()); <--- this line throws error shown below ...

Hiding Methods From Base Class
Is there a way to hide methods in a base class from a derived class so when the derived class is instantiated you can only see it's own methods? I don't have access to the base class but need to hide them. How about setting them to Private?Thanks, EdMicrosoft MVP - ASP/ASP.NET Did you mean a private method ? For example :Public Class myBaseClass Private Function doSome() As String Return "hello"End Function End ClassPublic Class derivedClass Inherits myBaseClass Public Function callDoSome() as string Return Me.doSome 'if you try to do this , you get a com...

How do I access methods from a class in App_Code
 I have added a class in App_Code that contains functions to format strings into certain formats.  I will be calling these from many of my .aspx classes.  What do I have to do to reference these from my .aspx class? <asp:Label ID="Label4" runat="server" Text='<%# FormatPhone(Eval("Phone")) %>'></asp:Label> The above works fine if the FormatPhone function is declared in the associated .vb file, but how do I reference the App_Code class/function?  Do I need an import in the .aspx?Thanks in advance for any hel...

Base class with access to context
As I'm writing my ASP.NET 2.0 application, I'm finding that I have several classes that are needing access to the HTTP Context.  I didn't plan ahead when I started building the classes, so now I'm having to go back and pass in the context from the pages that instantiate the object.I'm wondering if there is a good base class that is lightweight that I could have my classes inherit from that has access to the HTTP context object.AJAX Programming Tutorials System.Web.HttpContext.Current will allow you to access the current context object. System.Web.Page is the class that web forms inher...

Web resources about - accessing methods of base class - netscape.mozilla.jseng

Highest Percentage Of Opera Mini Users Accessing Facebook? Macau
If you were asked to guess which country had the highest percentage of users of Opera mobile Web browser Opera Mini users accessing Facebook ...

International Users Accessing Facebook Places Through US VPN Accounts
By using a virtual private network (VPN) hosted in the United States, Facebook users from around the world are accessing Facebook Places. The ...

What are some alternatives to Yodlee for accessing bank information?
Clay Loveless , Founder, Jexy. Co-founder, Mashery. Founder, Jexy. Co-founder, Mashery.

Cloud Console - Accessing files in cloud storage for iPad on the iTunes App Store
Get Cloud Console - Accessing files in cloud storage on the App Store. See screenshots and ratings, and read customer reviews.

Meryl Streep On Accessing The Characters Within - YouTube
Meryl Streep talks about the importance of an actors work representing their ability. CONNECT WITH AFI: http://facebook.com/AmericanFilmInstitute ...

Student pleads guilty to accessing records about Frances Abbott design scholarship
The Sydney student who leaked information about a fashion school scholarship controversially awarded to the daughter of the Prime Minister has ...

Accessing a headline opinion
Accessing a headline opinion

Sharp increase in authorities accessing private data
Australian law enforcement and government agencies have sharply increased their access without warrant to vast quantities of private telephone ...

Former librarian charged with accessing student records of Frances Abbott
A former part-time librarian at a Sydney design school has been charged after she allegedly accessed student records of Prime Minister Tony Abbott's ...

Frances Abbott scholarship: Sydney woman pleads guilty to accessing Whitehouse Institute records on PM's ...
A Sydney woman who leaked the student records of Tony Abbott's daughter pleads guilty. A Sydney woman who leaked the student records of Prime ...

Resources last updated: 12/16/2015 11:13:57 AM