Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JScipt Constuctor

Posted on 2013-05-25
1
Medium Priority
?
190 Views
Last Modified: 2013-05-25
I don't understand javascipt constructors!
I want an object with nested functions which takes a param in its creation, and that param is avail within the functions, e.g.
function ImLIBObject(colorToUse) {
        this.colorToUse = colorToUse;
   function ImNestedProc(e, oRow) {
       I want colorToUse here!!, i can't seem to get it, its undefined!!!
        }     

Open in new window

0
Comment
Question by:Silas2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
hielo earned 800 total points
ID: 39196771
Instead of:
function ImNestedProc(e, oRow){...}
you need to use the name of the function as a variable and assign it an anonymous function.
this.ImNestedProc=function (e, oRow){...};

You prefix it with "this." so that you can access it as a public method.
function ImLIBObject(colorToUse) {
        this.colorToUse = colorToUse;
	this.ImNestedProc=function (e, oRow) {
       alert( this.colorToUse );
        }
}

var x=new ImLIBObject('red')
x.ImNestedProc();

Open in new window


Another way to do this is to "attach" the function to the prototype chain:
//method
ImLIBObject.prototype.ImNestedProc=function(e, oRow) {
       alert( this.colorToUse );
}

//constructor
function ImLIBObject(colorToUse) {
        this.colorToUse = colorToUse;
}

var x=new ImLIBObject('red')
x.ImNestedProc();

Open in new window


With option 1, every time you create an instance of the object, each instance has its own copy/definition of the method.  With option 2, only one copy/definition of the method exists at any time regardless of how many instances of the object you create.

These should help you understand prototypes:
http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/
http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

705 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