tags:

views:

111

answers:

6

Please look at my required JavaScript.

var someVariable = new SomeDataType();

// I can directly access value of its property.
someVariable.someProperty = "test";
alert(someVariable.someProperty); // <- this command must should "test"

// However, I have some methods in above property

// I want to validate all rule in this property.
someVariable.someProperty.isValid(); // <- this method will return true/false

Is it possible for doing this in current version of JavaScript?

UPDATE

Please look as my answer!

A: 
someVariable.someProperty = [ test, anotherFunc, yetAnotherFunc];
someVariable.somePropertyAllValid= function() {
   for(var prop in someVariable.someProperty) {
      if(!prop()) return false;
   }
   return true;
};

someVariable.somePropertyAllValid();
eglasius
I can't do this because I have a lot of method or property inside this. like defaultValue, ruleCollection, etc.
Soul_Master
@Soul_Master if u mean u can't add all there, its an array so you can just add the functions as u go. On the other hand if u mean that those have args or things like that, u can define in line or assign to local variables ... pls explain "I can't do this ..."
eglasius
First, I need to directly access to someProperty value. Secondly, I have some methods and properties in this property. But your answer can't solve my question.
Soul_Master
+2  A: 

Yes, you can assign Javascript functions as properties like this:

someVariable.someProperty = function (arg1, arg2) {
  // function code goes here
};

This is the method using function literals.

Another method is to use function instances like this:

someVariable.someProperty = new Function (arg1, arg2, code);

Note that in the second method, the code goes in as the last parameter and the Function keyword has a capitalized 'F' as against method 1 where the 'f' is small.

Further, creating a function instance inside a loop etc. will create an entire new instance to assign which is inefficient in memory. This problem does not arise while using the function literal method.

Crimson
I don't understand. Can I directly access someVariable.someProperty value?
Soul_Master
After that, you can call someVariable.someProperty as if it were a function. As in var c=someVariable.someProperty(a,b)
notJim
It isn't my point. Your answer is only setting method into object.
Soul_Master
+2  A: 

You can't (and probably shouldn't) treat objects like that in JavaScript. As someone else mentioned, you can override the toString() method to get half of the functionality (the read portion), but you cannot use the assignment operator on an object like that without overwriting the object.

You should choose a different approach, like using nested objects (as CMS suggested).

Justin Johnson
I agree with this opinion.
Soul_Master
Then mark it as the answer mate ;)
Justin Johnson
There are 2 different approachs like Ramesh Vel and I posted it. Thank.
Soul_Master
A: 

I just found the answer. It's very simple & clean.

function createProperty(value, defaultValue, ruleCollection)
{
    this.value = value;
    this.defaultValue = defaultValue;
    this.ruleCollection = ruleCollection;       
}

createProperty.prototype.toString = function()
{
    return this.value;
};

var someVariable = 
{
    someProperty: new createProperty
    (
        'currentValue',
        'defaultValue',
        null
    )
};

For testing, you can use something like my following code.

var test = ">>" + someVariable.someProperty + "<<";

// this alert must shows ">> currentValue <<"
alert(test);


someVariable = 
{
    someProperty: new createProperty
    (
        7,
        5,
        null
    )
};

test = someVariable.someProperty + 3;

// This alert must shows "10"
alert(test);

I just test it on FF 3.5 & IE 8. It works fine!

Update

Oops! I forget it. Because this technique returns object reference for this property. So, it's impossible to directly set property data. It isn't my final answer.

Soul_Master
+2  A: 

Its possible, but with the below change in your code

function SomeDataType(){
 var localProperty="";
 this.someProperty = function(txt){
  if (arguments.length==0)
   return localProperty;
  else
   localProperty=txt;
 }  
 this.someProperty.isValid = function(){
   return (localProperty!="") ? true : false;

 };
}

instead of defining someProperty as a property, define this as function which sets value to the local property if any value is passed or it ll return that property value if no argument is given.

var someVariable = new SomeDataType();
someVariable.someProperty("test");
alert(someVariable.someProperty()); 

var isValid = someVariable.someProperty.isValid();

this is how you need to access the SomeDataType object.

Cheers

Ramesh Vel

Ramesh Vel
Sorry. Please try to look at my answer.
Soul_Master
@Soul_master, i just checked your answer. i dont find anywhere you can access isValid of the property of the someProperty. thats impossible. thats why i gave you this workaround..
Ramesh Vel
I don't add into someProperty because the point of this problem is how to directly access to property that has some inner property and some inner method.
Soul_Master
+1 Your answer is another way to solve this question. However, getting and setting of this answer use method for doing it. But my question want to use it as property.
Soul_Master
A: 

Perhaps this would be of help:

var SomeVar = {
  someProperty : {
    value : 7,
    add : function (val) {
      this.value += parseInt(val, 10);
      return this;
    },
    toString : function () {
      return this.value;
    }
  }
}

alert(SomeVar.someProperty.add(3));
Robert Cabri