Syntax new with a object

Hi,
I saw for instance


new google.translate.TranslateElement

but I could do only google.translate.TranslateElement


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 ?

You have to remember that JQuery itself is just a collection of objects written in JavaScript.

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


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 :slight_smile:

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

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”.