Solved

Javascript prototype problem

Posted on 2014-03-24
5
188 Views
Last Modified: 2014-03-25
I'm floundering with TypeScript and javascript.
Basically, I'm putting all my UI state onto a single object defined in Typesript.
Typescript:
module AllAppState {
    export class Appsd {
        public OrdersViewBy() {
            return GetViewFromCookie('OrderTypeViewOptions', $("#cboOrderTypes").val());
        }
        public MonthsBack() {
            //debugger;
            return GetViewFromCookie('GoBackMonths', 1);
        }
        public CustomerID() { return $("#txtCustomerID").val(); }

    }
}

Open in new window


for those not seen typescript, this compiles to:
var AllAppState;
(function (AllAppState) {
    var Appsd = (function () {
        function Appsd() {
        }
        Appsd.prototype.OrdersViewBy = function () {
            return GetViewFromCookie('OrderTypeViewOptions', $("#cboOrderTypes").val());
        };
        Appsd.prototype.MonthsBack = function () {
            //debugger;
            return GetViewFromCookie('GoBackMonths', 1);
        };
        Appsd.prototype.CustomerID = function () {
            return $("#txtCustomerID").val();
        };
        return Appsd;
    })();
    AllAppState.Appsd = Appsd;
})(AllAppState || (AllAppState = {}));
//# sourceMappingURL=AppState.js.map

Open in new window

Then in the calling code I'm tyring to dynamically call these procedures but I can't seem to get to them...basic javascript I know, but...
for (pieceAppState in propertyValue()) {
    dataToSend[propertyName] = pieceAppState();//Can't call functions here!!!!
}

Open in new window

0
Comment
Question by:Silas2
  • 3
  • 2
5 Comments
 

Author Comment

by:Silas2
ID: 39950631
What I don't understand is that all the pieceAppState in propertyValue() (which returns the object Appsd) are strings, not functions, but they are defined as functions in the object e.g. Appsd.prototype.CustomerID = function (), how do I get to the CustomerID() function (without hard-coding which does work)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39952841
Perhaps you mean

var ID = propertyValue[pieceAppState].CustomerID();
0
 

Author Comment

by:Silas2
ID: 39952963
The problem with that is that I have to hard-code the 'CustomerID();' bit, I wanted a way to iterate the prototype.myFuncs= function() and call each one in turn without hardcoding the names.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 300 total points
ID: 39953040
for (var obj in someCollection) {
  if (typeof someCollection[obj] === "function") someCollection[obj]();
}
0
 

Author Comment

by:Silas2
ID: 39953071
By jove you're right!
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question