Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How to deal with Uncaught TypeError?

How to deal with Uncaught TypeError?

Scheduled Pinned Locked Moved JavaScript
toolshelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Ted K 1
    wrote on last edited by
    #1

    /*
    Dear all,

    <!-- code -->
    <script>
    function Array2D(x,y){
    this.length = x*y;
    this.x = x;
    this.y = y;

            for(var i=0 ; i<this.length ; i++){
                this\[i\] = null;  
            }
            this.get = function(x,y){ 
                return this\[x\*this.x+y\];
            }
            this.set = function(x,y,value){ 
                this\[x\*this.x+y\] = value;
    
            }
        }
    

    </script>

    <script>
    var a2d = Array2D(10,10);
    a2d.set(2,3,"2");
    alert(this.get(1,2));
    </script>
    <!-- end code -->

    <!-- error message -->
    Uncaught TypeError: Cannot read property 'set' of undefined

    How does it happen?and what should i do to avoid this?
    */

    W 1 Reply Last reply
    0
    • T Ted K 1

      /*
      Dear all,

      <!-- code -->
      <script>
      function Array2D(x,y){
      this.length = x*y;
      this.x = x;
      this.y = y;

              for(var i=0 ; i<this.length ; i++){
                  this\[i\] = null;  
              }
              this.get = function(x,y){ 
                  return this\[x\*this.x+y\];
              }
              this.set = function(x,y,value){ 
                  this\[x\*this.x+y\] = value;
      
              }
          }
      

      </script>

      <script>
      var a2d = Array2D(10,10);
      a2d.set(2,3,"2");
      alert(this.get(1,2));
      </script>
      <!-- end code -->

      <!-- error message -->
      Uncaught TypeError: Cannot read property 'set' of undefined

      How does it happen?and what should i do to avoid this?
      */

      W Offline
      W Offline
      Wombaticus
      wrote on last edited by
      #2

      It's undefined because you ahven't returned anything form your Array2D function. Try adding

      return this;

      at the end of it. Also, your

      alert(this.get(1,2));

      shouold be

      alert(a2d.get(1,2));

      T 1 Reply Last reply
      0
      • W Wombaticus

        It's undefined because you ahven't returned anything form your Array2D function. Try adding

        return this;

        at the end of it. Also, your

        alert(this.get(1,2));

        shouold be

        alert(a2d.get(1,2));

        T Offline
        T Offline
        Ted K 1
        wrote on last edited by
        #3

        thanks a lot! it works! ;)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups