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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    georgia,usa
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Object Constructor Confussion

    I have been able to get this javaScript to work using Arrays , but when I rewrite all the code to attempt it by creating new objects with an 'Object constructer function' I get lots of "not an object" and "undefined" errors.

    My browser is ie5.0, the editor is Evrsoft 1stPage2000. Does my editor support new Object creation ?

    The Evrsoft editor has an option that is supposed to let you open files in another browser but it doesn't seem to work. Anyone know anything about that ?

    So assumming it's not the editor browser's fault. Here's the javaScript that's not going so hot.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    	<title>TestQuip</title> 
    <script language="javascript1.2" type="text/javascript">
    <!-- Invi script
       function Test(question,choiceList){
         this.question = question;
         this.choiceList = choiceList;
         this.insertAnswers = insertAnswers;
    	 }
        var question01 = new Test("What is the glarp question \?","darn");
        var question02 = new Test("Who let the dawgs out \? glarp","Who Who \!");
     
       function insertAnswers()
          {
    	  var Q = this.question.value;
    	  var g = Q.indexOf("glarp");
                   frontHalfQuestion = Q.substring(0,g);
    	  var ball = g+5;
    	  backHalfQuestion = Q.substring(ball,Q.length);
    	  return frontHalfQuestion,choiceList,backHalfQuestion
          }
     // preemo -->
     </script> 
     </head> 
    <body>
     for (j=0; j<=Test.length-1;j++)
       {
    document.write('<P>Question ',j+1,' of ',Test.length,'</P>');// writes "question 1 of 2"
      insertAnswers(this.question,this.choiceList);//worked when it was Arrays and question[j]
      document.write'<P>',frontHalfQuestion+choiceList+backHalfQuestion,'</P>');
        }
     </script>
     </body>
     </html>
    the hard part for me is understanding the "instance of" stuff. I've read, studied, all the FAQ's and tutorials here. They helped me realize what the problem wasn't.

    I thought the whole Idea behind a construtor function like the one above was that 'Test' would become a new Object , 'question' would become a property of 'Test' , would have a value and be defined. In this case Test.question.value would equal "What is the glarp question \?". My machine doesn't return that, I get the error message 'Test.question is not an object'.

    so, before KOR rips me for being too wordy, I've tried to access the values of the 'question' property a hundred ways.
    Test.question.value
    Test["question"].value
    Test[0].question
    Test.this.question
    Test[0].question[0].value
    this.question
    question
    question[0]
    Test.prototype.question
    nothing has worked

    I found that Test.length works. (returns 2 like it should) .

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought the whole Idea behind a construtor function like the one above was that 'Test' would become a new Object , 'question' would become a property of 'Test'
    Given

    question01 = new Test("What is the glarp question \?","darn");

    question01 is now an object of type Test. Test doesn't care and doesn't know anything about question01; Test remains a function/constructor. To access the properties of the object-instance known as question01, you use the same dot-notation or square-bracket-notation you've used with other non-custom objects:

    question01.question // gives the string "What is the glarp question \?"
    question01.choiceList // gives the string "darn"
    question01['choiceList'] // also give "darn"

    To access an object's method, you also need to use dot-notation!

    question01.insertAnswers() // results in an error, see below

    Since your insertAnswers() function exists as its own independent function (as well as being appended to the Test-objects as a method), you can call it directly. But that will result in errors since this inside it won't have any meaning. I should also point out that you forgot to put this. before choiceList in your function, and that your return is broken.

    I'm not so good at explaining, so have some code! In your odd example here, I suggest putting your objects into an array for easy access. I've also patched up insertAnswers().
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    	<title>TestQuip</title> 
    <script type="text/javascript">
    function Test(question,choiceList){
      this.question = question;
      this.choiceList = choiceList;
      this.insertAnswers = insertAnswers;
      }
    
    var question=[]
    question[0] = new Test("What is the glarp question \?","darn");
    question[1] = new Test("Who let the dawgs out \? glarp","Who Who \!");
     
    function insertAnswers(){
      var Q = this.question;
      var g = Q.indexOf("glarp");
      frontHalfQuestion = Q.substring(0,g);
      var ball = g+5;
      backHalfQuestion = Q.substring(ball,Q.length);
      return frontHalfQuestion+this.choiceList+backHalfQuestion
      }
    </script> 
    </head> 
    
    <body>
    <script type='text/javascript'>
    for (j=0; j<question.length;j++){
      document.write('<p>Question ',j+1,' of ',question.length,'</p>');// writes "question 1 of 2"
      document.write('<p>',question[j].insertAnswers(),'</p>');
      }
     </script>
     </body>
     </html>
    Last edited by Harry Armadillo; 05-27-2005 at 12:27 AM. Reason: darned typos!

  • #3
    New Coder
    Join Date
    May 2005
    Location
    georgia,usa
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Harry You Rock.

    I was just getting discouraged with coding forums and you came up with the perfect pick-me-up. I think nursing newbies along gets a little on the nerves of these people that have been following JavaScript since it's electronic infancy. They've probably forgotten more code than I can learn in four years. Well thank-you :dance: I thought I wasn't going to get an answer because I mentioned EvrSoft. I get a feeling there's some politics going on there. Personally I rather deal with a hard truth and find a work arround than be deaf. I like my page editor, I just wish I had more documentation on it's abilities. It all seams so 'proprietary'. I bet some of these superScripters have some slick editors.

    Thank You Again,

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not aware of any politics about slick-editors, though I do get annoyed by the presence of custom/proprietary tags in posted code fragments. Especially when these unknown and unexplained bits seem vital to the debuggin'. But, I digress...

    Custom objects and constructors aren't something that'll you need very often, but I find them to be fun. So ask all the questions you want. I may not be able to explain very well, but I'll try.

    FWIW, I use a simple text editor called scite for all my 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
    •