Welcome to WebmasterWorld Guest from 3.209.80.87

Forum Moderators: open

Message Too Old, No Replies

arguments array

is it accessible from a class constructor

     
12:21 am on Aug 24, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


I'm trying to build a class constructor with a method that builds an unordered list of object properties. It fails on the commented out lines but works with the live for and switch statements below:

function User(forename, username, password){
this.forename = forename;
this.username = username;
this.password = password;
this.showUser = function(){
var myul = document.createElement("ul");
//for(var i=0;i<User.arguments.length;i++){
for(var i=0;i<3;i++){
//switch(User.arguments[i]){
switch(i){
case 0:


The arguments array is null and I'd only be guessing as to why. My guess is that the array is created when the object.properties are initialized, but when the method is invoked the array is empty? Thanks all!
8:20 am on Aug 24, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


//for(var i=0;i<User.arguments.length;i++){


The problem here is that the 'arguments' property of a function is not accessible externally. When you refer to User.arguments, that is a reference to an external property of User which is not publicly available. You can create an externally referenceable static property for User (User.lastArguments = arguments;) from within the constructor, though this is not so great as it gets overwritten every time a new User is created. Else you could create a localized var to reference later from privileged members [javascript.crockford.com] such as showUser. Such as:

function User(forename, username, password){
var args = arguments; //private variable
this.forename = forename;
this.username = username;
this.password = password;
this.showUser = function(){ //privileged member
var myul = document.createElement("ul");
for(var i=0;i<args.length;i++){
alert(args[i]);
}
};
}

var shmo = new User('Joe', 'joeshmo', 'shmopass');
shmo.showUser();
1:49 pm on Aug 24, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


Try using arguments in place of User.arguments

[seifi.org...]
3:41 pm on Aug 24, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 7, 2010
posts: 119
votes: 0


astupidname: Thanks, I decided to use the local var myargs that my 'privilaged' member this.showUser has access to (if I'm comprehending correctly); and, User.arguments is only accessible from within the function that owns it and NOT even from functions it owns (privilaged functions)? I bookmarked your page, excellent info. My first javascipt constructor:

<div id="results1"></div>
//
window.onload=function(){
var myobject = new User("richard", "nyteshade", "secret");
myobject.showUser();
}
function User(forename, username, password){
var myargs = arguments;
this.forename = forename;
this.username = username;
this.password = password;
this.showUser = function(){
var myul = document.createElement("ul");
for(var i=0;i<myargs.length;i++){
switch(i){
case 0:
var mycontent = "Forename : " + this.forename;
break;
case 1:
var mycontent = "Username : " + this.username;
break;
case 2:
var mycontent = "Password : " + this.password;
break;
}
var myli = document.createElement("li");
var mytextnode = document.createTextNode(mycontent);
myli.appendChild(mytextnode);
myul.appendChild(myli);
myid = document.getElementById("results1");
myid.appendChild(myul);
}
}
}