Web Analytics Made Easy -
StatCounter can I create an array of objects? - CodingForum

Announcement

Collapse
No announcement yet.

can I create an array of objects?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • can I create an array of objects?

    How Object oriented is Javascript? I am still learning at the very beginning stages, but I was wondering if I could create an array of objects. Namely objects of a type I create.

    Which leads me to, is that possible? And if so, how do I create an object (syntax wise I mean), and what is the syntax to create an array like that.

  • #2
    See this simple example:
    Code:
    <script type="text/javascript">
    function Person(name, age){
      this.name=name;
      this.age=age;
      this.talk=function(){
        alert('Hello! I am ' + this.name + ' and I am '+this.age+' years old.');
      }
    }
    
    var p = new Person("John",25);
    var p2 = new Person("Glenn",27);
    
    var arrP = new Array(p, p2);
    for (var i=0;i<arrP.length;i++) {
      arrP[i].talk();
    }
    </script>
    For more info on OOP in Javascript, read these 2 articles:
    JavaScript is a programming language that is primarily used to create interactive and dynamic website content. It can be used to manipulate the Document Object Model (DOM) in a web page, making it a popular choice for creating dynamic user interfaces and web applications.

    JavaScript is a programming language that is primarily used to create interactive and dynamic website content. It can be used to manipulate the Document Object Model (DOM) in a web page, making it a popular choice for creating dynamic user interfaces and web applications.
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      Thanks

      Comment

      Working...
      X