SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Syntax new with a object

    Hi,
    I saw for instance
    Code JavaScript:
    new google.translate.TranslateElement

    but I could do only google.translate.TranslateElement

    Code JavaScript:
    var my = {name:'mytest'};
    my.mytest = function() {
        alert(this.name);
    }
     
    my.mytest();
    // I got mytest
    var o = new my.mytest();
    //I got undefined

    What's the difference and what's the use of it ?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your variable "my" is an associative array. That is it an array where you can access the elements by a string. Your variable "my" is defined a an array with a single element access by "name" with a value the string "mytest".

    You add another element to the array with the statement

    Code JavaScript:
    my.mytest = function() {
        alert(this.name);
    }

    This element of the array is accessed by "mytest" and has a value of function. That is whenever the element is accessed the function is executed. The value return by the function is the value of that array element.

    Your statement var o = new my.mytest(); is causing the function to be executed, which is showing the value of this.name. "this" properly exists as an object but because "name" is not a property of that object you get the "undefined".

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply but actually associative array
    in js are object
    http://www.quirksmode.org/js/associative.html

    My post was about the syntax.

    I think I found an answer

    Code JavaScript:
    var my = function(p ) {
      return new my.fn.mytest( p );
    }
    my.fn = my.prototype = {
      mytest: function( p ) {
        alert(p);
      }
    }
    my('mytest');

    like jquery

    It seems object notation

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,807
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by whisher View Post
    like jquery

    It seems object notation
    You have to remember that JQuery itself is just a collection of objects written in JavaScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •