Jeremy Belolo Jeremy Belolo - 2 months ago 13
Javascript Question

Jquery - $.extend not returning expected functions

I'm using gmail.js for some project. In the library, there is a function like this :

api.dom.compose = function(element) {
// stuff
}

api.dom.email = function(element) {
this.id = element;
var message_class_id = 'm' + this.id;
this.id_element = $('div.ii.gt div.a3s.aXjCH.' + message_class_id);
element = this.id_element.closest('div.adn');

this.$el = element;
return this;
};

$.extend(api.dom.email.prototype, {
body: function(body) {
var el = this.dom('body');
if (body) {
el.html(body);
}
return el.html();
},

from: function(email, name) {
var el = this.dom('from');
if (email) {
el.attr('email',email);
}
if (name) {
el.attr('name',name);
el.html(name);
}
return {
email: el.attr('email'),
name: el.attr('name'),
el: el
};
},

// more extended functions

});

// more functions on the api.dom object

return api;


In my code I'm using it like so :

var email = provider.dom.email(mId);
console.log(email);


The
console.log
is really surprising. I was expecting to see the functions from the
$.extend
section. In that place, the functions showing are those registered on the
api.dom
object !
email()
itself,
compose
, and more.

I don't get at all why this is happening. Thanks ahead for any help.

Answer

It was the prototype that has been extended. The functions are available when creating an instance with new. So do a console.log(api.dom.email.prototype); or create a new instance with new.

var email = new provider.dom.email(mId);
console.log(email);
Comments