This is one of the standard way of defining class in JavaScript. Can you explain your problem little more? You can also try this-
<script type="text/javascript">
function Hero(propertyobject) {
for (var p in propertyobject) {
this[p] = propertyobject[p];
}
this.whoAreYou = function () {
return "I'm " + this.name + " and I'm a " + this.occupation;
}
}
var c = new Hero({ "first": 1, "second": 3 });
alert(c.first);
</script>
Thanks & Regards
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
These classes are use for sending filter’s selected items from the UI to the Server (when a user applies a filter - Here filter is user settings) and from the Server to the UI (when we are applying a pre-saved or default filter for the user; i.e. when we
are populating the UI).
The communication between these classes would be through the web service Standard.svc using JSON.
So, an object from the class classFilter in the UI, containing items selected by the user, is send to the Server, where it is converted to an object of Entity.Filter.
Here I want to make simple property class in JS like we do in CSharp to save values.
Please go through above requirement and give some optimized solution.
In that case you need not to create a class in JavaScript. For the UI you can create a object on the fly like-
var obj=new Object();
And then based on the user selection from UI you can populate the property like obj["PropertyName"]. Property name should match property of C# class.
May be you can apply a css class to all the control and using jquery each function you can loop though all the control and create the javascript. While sending the object you can serialize the object and send the value to .svc service.
If i've understood your requirement correctly think that asteranup has already given you the answer, you're maybe just taking his code a bit too literally?
Is this what you want
<script language="Javascript">
function myClass() {
this.Prop1 = ''
this.Prop2 = ''
}
var test = new myClass();
test.Prop1 = 'One';
test.Prop2 = 'Two';
alert(test.Prop1 + ', ' + test.Prop2);
</script>
dotnet_CH
Member
40 Points
259 Posts
How to define Javascript properties
Nov 16, 2012 09:25 AM|LINK
Hi All
Code:
function clsTimeFrame() {
this.StartDateFilter;
this.EndDateFilter;
};
How to define properties in javascript class - Is anything is required to initailized them. Or this is the right way to define them.
Please give your inputs on this - I want to define property class same on the line of Property Class in CSharp.
asteranup
All-Star
30184 Points
4906 Posts
Re: How to define Javascript properties
Nov 16, 2012 09:34 AM|LINK
Hi,
You can try this way-
function Hero(name, occupation) { this.name = name; this.occupation = occupation; this.whoAreYou = function() { return "I'm " + this.name + " and I'm a " + this.occupation; } }you can study any object oriented JavaScript concepts-
https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript
http://www.slideshare.net/rmsguhan/javascript-design-patterns
http://net.tutsplus.com/tutorials/javascript-ajax/digging-into-design-patterns-in-javascript/
You can also study this book-
http://www.wowebook.be/book/object-oriented-javascript/
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
dotnet_CH
Member
40 Points
259 Posts
Re: How to define Javascript properties
Nov 16, 2012 09:36 AM|LINK
i am going to define large number of properties in JS Class - therefore I cannt define in constructor of the class.
Help in define JS Class to define large properties - I am going to use them entity class to store large info about object.
asteranup
All-Star
30184 Points
4906 Posts
Re: How to define Javascript properties
Nov 16, 2012 09:53 AM|LINK
Hi,
This is one of the standard way of defining class in JavaScript. Can you explain your problem little more? You can also try this-
<script type="text/javascript"> function Hero(propertyobject) { for (var p in propertyobject) { this[p] = propertyobject[p]; } this.whoAreYou = function () { return "I'm " + this.name + " and I'm a " + this.occupation; } } var c = new Hero({ "first": 1, "second": 3 }); alert(c.first); </script>Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
dotnet_CH
Member
40 Points
259 Posts
Re: How to define Javascript properties
Nov 16, 2012 09:58 AM|LINK
Thanks anup for your kind help and patience.
My requirement is like this:
These classes are use for sending filter’s selected items from the UI to the Server (when a user applies a filter - Here filter is user settings) and from the Server to the UI (when we are applying a pre-saved or default filter for the user; i.e. when we are populating the UI).
The communication between these classes would be through the web service Standard.svc using JSON.
So, an object from the class classFilter in the UI, containing items selected by the user, is send to the Server, where it is converted to an object of Entity.Filter.
Here I want to make simple property class in JS like we do in CSharp to save values.
Please go through above requirement and give some optimized solution.
asteranup
All-Star
30184 Points
4906 Posts
Re: How to define Javascript properties
Nov 16, 2012 10:12 AM|LINK
Hi,
In that case you need not to create a class in JavaScript. For the UI you can create a object on the fly like-
And then based on the user selection from UI you can populate the property like obj["PropertyName"]. Property name should match property of C# class.
May be you can apply a css class to all the control and using jquery each function you can loop though all the control and create the javascript. While sending the object you can serialize the object and send the value to .svc service.
Check this.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
clarky
Participant
918 Points
174 Posts
Re: How to define Javascript properties
Nov 16, 2012 10:16 AM|LINK
Hi,
If i've understood your requirement correctly think that asteranup has already given you the answer, you're maybe just taking his code a bit too literally?
Is this what you want
<script language="Javascript"> function myClass() { this.Prop1 = '' this.Prop2 = '' } var test = new myClass(); test.Prop1 = 'One'; test.Prop2 = 'Two'; alert(test.Prop1 + ', ' + test.Prop2); </script>HTH
dotnet_CH
Member
40 Points
259 Posts
Re: How to define Javascript properties
Nov 18, 2012 09:19 AM|LINK
Thanks lot.
dotnet_CH
Member
40 Points
259 Posts
Re: How to define Javascript properties
Nov 18, 2012 09:20 AM|LINK
Thanks