Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Learning OOP in JavaScript

    I learn OOP in javascript. and a few articles say that we can create a static prop/meth to an object just like a predefined object. But i think it just a prop/meth assingned to a constructor. Or is there really static meth/pro in javascript?.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    It depends on what they mean by "static" method/property.

    I have a different vision about that. From the javascript point of view, elements have native methods/properties, but, if treated like ordinary objects, the may gain other custom methods/properies as well.

    For instance, an input has, among other native properties, the native property type:
    <input id="myinput" type="text">
    so that
    var myPropertyValue=element.type will return the value of the type property.

    But, as any other element/property/method/variable, that input may be treated as an object, and you may assign to that element a custom property, let's say required. When assigning a custom property, you must take care not to use a javascript reserved word, and is better to avoid native properties names as well.
    element.required='yes';
    var myPropertyValue=element.required;

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <script type="text/javascript">
    /* <![CDATA[ */
    onload=function(){
    var e=document.getElementById('myinput');
    e.required='yes';
    alert(e.required)
    }
    /* ]]> */
    </script>
    </head>
    <body>
    <form action="">
    <div>
    <input id="myinput" type="text" />
    </div>
    </form>
    </body>
    </html>
    This a dynamic way to assign a new property to the elements. It is true that you may do that in a static way as well:
    <input id="myinput" type="text" required="yes">
    It will work, but two things are to be considered in this case:
    1. Even it works in all the modern browsers, the static custom property is not considered a valid HTML code by the validators
    2. In full DOM compliant browsers (Mozilla...) the new property is not to be found, in this case, on using the classical read method:
    var myPropertyValue=element.required
    as it is not a native property. But the DOM 1 or, even better, the DOM 2 level methods are to be used:
    var myPropertyValue=element.getAttributeNode('required').nodeValue;
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <script type="text/javascript">
    /* <![CDATA[ */
    onload=function(){
    var e=document.getElementById('myinput');
    alert(e.getAttributeNode('required').nodeValue)
    }
    /* ]]> */
    </script>
    </head>
    <body>
    <form action="">
    <div>
    <input id="myinput" type="text" required="yes"/>
    </div>
    </form>
    </body>
    </html>
    I hope it helps...
    Last edited by Kor; 01-10-2007 at 12:13 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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