Drycodez 4 Posted October 5, 2011 Report 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? Quote Link to post Share on other sites
Ingolme 1,020 Posted October 5, 2011 Report Share Posted October 5, 2011 Only if you actually need to have properties on a function. 1 Quote Link to post Share on other sites
Don E 125 Posted October 5, 2011 Report 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. Quote Link to post Share on other sites
Drycodez 4 Posted October 5, 2011 Author Report 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). Quote Link to post Share on other sites
EricPinxteren 1 Posted October 6, 2011 Report Share Posted October 6, 2011 (edited) 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; Edited October 6, 2011 by EricPinxteren 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.