Results 1 to 9 of 9

Thread: Property procedures in java

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769

    Property procedures in java

    How do you write a property procedure in java?

    I know that I would use something like this to get a property:

    Code:
    var myprop = 'my property string'
    
    function getProperty(){
        getProperty = myprop;
    }
    but how do I set the property. At the moment I am using this:

    Code:
    var myprop = 'my property string'
    
    function setProperty(newvalue){
        myprop = newvalue;
    }
    
    setProperty('this is the new value');
    I don't want to have to do it this way. Is it not possible to do it like in vb, something like:

    Code:
    setProperty = 'this is the new value';

  2. #2
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Not quite sure what you are asking. What you've written so far is way more complicated than neccessary.

    Also, note that you have written JavaScript, not Java--there's a difference. In the Java language it's customary to write accessor functions (more or less similar to what it appears you are doing) to set and get properties (aka member variables), but in JavaScript I've never seen it done. JavaScript, however, does have some object-oriented capabilities that most people ignore or overlook. I've been taking advantage of these capabilities in my JavaScript code and have been pleased with the results.

    So what is it that are you trying to do?

    cudabean

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    I have created a type of object in jscript. I have local variables which I would like to manipulate using a vb style property functions.

    I want to be able to use the functions in the following 2 ways.

    1) object.property = newvalue
    2) alert(object.property)

    ie I would like to use the same procedure to assign and retreive values, but don't know the syntax for the function/functions.

    At the moment I would have to have two different functions.
    1) setProperty(newvalue)
    2) getProperty()

    and call them as such
    1) object.setProperty(newvalue)
    2) alert(objcet.getProperty)

    It makes my object look messy.

  4. #4
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Here's the simplest way to manipulate objects and properties in JavaScript:

    var myObj = new Object;
    myObj.prop1 = "this is the first property";
    myObj.prop2 = "this is the second property";
    myObj.prop3 = "this is the third property";

    The simple idea here is once you have an Object, you don't need to declare properties you just start using them as if they were already declared.

    Now we can do interesting things with myObj:

    newObj = myObj;

    alert(newObj.prop1); // prompts: this is the first property
    alert(newObj.prop2); // prompts: this is the second property

    We can do even stranger things:
    myObj.prop4 = myObj;

    alert(myObj.prop4.prop4.prop4.prop2); // prompts: this is the second property

    That last one, although legal, is pretty useless, but it shows how you might implement a linked list in JavaScript.

    Let me know if this has helped any. The thing I can't answer is how to create methods to go along with your objects in JavaScript.

    cudabean

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    yes this helps a bit. My next problem here is how do I use this property within the object?

    For instance, here is my object

    Code:
    function MyObject(){
    
        function runMethod(){
            alert('Hello');
        }
    
        this.method = runMethod;
    }
    I now want to use the value of my property (prop) within the function. How do I do this?

    Code:
    function MyObject(){
    
        function runMethod(){
            alert('Hello ' + prop);
        }
    
        this.method = runMethod;
    }
    
    var newobj = new MyObject();
    newobj.prop = 'shunt';
    newobj.runMethod();
    Which returns 'Hello shunt'?

    Also, how do I manipulate other values when this property is assigned. In vb we can do this in the property procedures.
    In the eg above, we won't be able to do this. Also no validation on property input. How do I code my object to handle this?

  6. #6
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    I haven't played with OO in JavaScript, but I suggest some of the links in my sig.

    Aside from that (and I know this has been clarified)...

    JavaScript is not the same as Java!
    Javascript is not the same as JScript!
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  7. #7
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    I believe you are asking too much from JavaScript. I can see where you are going, but I don't think it will do it. Now, while it IS legal to nest functions in JavaScript, the purpose is to make the nested functions available to the parent function. The nested functions have no scope beyond the confines of the parent! (the nested functions have access to the parents variables, but the parent can't access the nested functions variables).

    Another thing, I read somewhere that JavaScript can't really be considered OO. It has some OO-like features, but it fails on most of the criteria used to define OO languages.

    Cudabean

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769
    The nested functions have no scope beyond the confines of the parent! (the nested functions have access to the parents variables, but the parent can't access the nested functions variables).
    This is my goal. I am trying to make some generec search boxes, calendar controls etc. in HTML to be used on the intranet. It is working quite well, but I need to encapsulate some of the methods so that they are not broken by other things I may have on my pages.

    The other point is re-usability.

  9. #9
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Hmmm. I guess you can do some object oriented stuff in JavaScript:

    Code:
    Pet = function( name )
    {
        this.name = name;
    }
    Dog = function( name )
    {
        // make Pet a method of the instance
        this.$_base = Pet;
        // run Pet from here, so its 'this' value 
        // will equal the instance
        this.$_base( name );
        // clean up this incestuous method!
        delete this.$_base;
    }
    Dog.prototype.__proto__ = Pet.prototype;
    
    rover = new Dog( "Rover" );
    // test
    trace(rover.name); // Rover
    I got this from:
    http://www.debreuil.com/docs/ch01_Proto.htm
    (Note, it says it's flash 5, but the information therin applies to JavaScript.)

    cudabean

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width