Drycodez Posted October 5, 2011 Share Posted October 5, 2011 function a(){}a.name="You"a.age;alert(a.name)//prints: 'You'. is it a good idea to defined object this way? Link to comment Share on other sites More sharing options...
Ingolme Posted October 5, 2011 Share Posted October 5, 2011 Only if you actually need to have properties on a function. Link to comment Share on other sites More sharing options...
Don E Posted October 5, 2011 Share Posted October 5, 2011 I learned that when doing what you did Sam, that's what's considered a 'constructor' for an object in JavaScript. Link to comment Share on other sites More sharing options...
Drycodez Posted October 5, 2011 Author Share Posted October 5, 2011 I learned that when doing what you did Sam, that's what's considered a 'constructor' for an object in JavaScript. Yeah, but its still an object. Using the " new" keyword, just create a clone (copy/instance/prototype). Link to comment Share on other sites More sharing options...
EricPinxteren Posted October 6, 2011 Share Posted October 6, 2011 Mmm.. it does not make much sense. Maybe if you put some prototype function in the function (constructor) like this: function a(name,age){ this.name = name; this.age = age;}a.prototype = { alert:function() { alert(this.name+" is "+this.age+" years old") }, giveAge:function() { return this.age; }};var me = new a("eric",25);me.alert();//shows eric is 25 years old Then you can make multiple a's, and they have all the same functions. If you just want make object for holding vars, you can als do this: //Like this:var a = {name:"eric",age:25}; //or like this: var a = {}; a.name = "eric";a.age = 25; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.