SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does the author refer to as "newly created object" in this code?

    This is part of Ryan Frishberg's article on JavaScript: http://www.sitepoint.com/oriented-programming-1/

    Code:
    function myFunc(StringValue){  <br>  
          this.StringValue = StringValue;  <br>  
        }  <br>  
          <br>  
        var myObject = new myFunc("This is myObject's string");  <br>  
        var myObject2 = new myFunc("This is a String");  <br>  
        alert(myObject.StringValue); // displays "This is myObject's string"  <br>  
        alert(myObject2.StringValue); // displays "This is a String"
    He writes, "In the myFunc() constructor, this.StringValue refers to the property being assigned to the newly created object, while StringValue refers to the functionís local variable that was passed as an argument."

    What does he refer to as "newly created object"?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Grafica View Post
    What does he refer to as "newly created object"?
    With the following code:

    Code javascript:
     var myObject = new myFunc("This is myObject's string");

    The myObject variable contains the newly created object.

    So with the myFunc constructor, where it uses this.StringValue will result in the myObject variable having a property called myObject.StringValue

    I would rewrite the code so that it follows standard coding conventions where only the constructor function has a capital first letter and all other variables do not, for example:

    Code javascript:
    function Kitten(name) {
        this.type = 'cat';
        this.name = name;
    }
     
    var kitten = new Kitten('Socks');
    alert(kitten.name); // displays 'Socks'
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would rewrite the code so that it follows standard coding conventions where only the constructor function has a capital first letter and all other variables do not, for example:

    Code javascript:
    function Kitten(name) {
        this.type = 'cat';
        this.name = name;
    }
     
    var kitten = new Kitten('Socks');
    alert(kitten.name); // displays 'Socks'
    Thanks. BTW, does
    Code:
    this.name = name;
    say that the new constructor (new Kitten) will have the same name as function Kitten?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Grafica View Post
    Thanks. BTW, does
    Code:
    this.name = name;
    say that the new constructor (new Kitten) will have the same name as function Kitten?
    It says that the new object being created will have a property called name, which is assigned the value of name which is passed in to the constructor.

    So:

    Code javascript:
    var kitten = new Kitten('Socks')

    results in 'Socks' being passed as a parameter to the Constructor function called Kitten, so that in:

    Code javascript:
    [function Kitten(name) {
        ...
    }

    the variable called name is now the string 'Socks'

    When this next line of code is executed

    Code javascript:
    this.name = name;

    The this keyword refers to the object being constructed, and this.name is a property of that object that we are assigning a value to, that value being 'Socks'
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.


Tags for this Thread

Bookmarks

Posting Permissions

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