0

I'm just starting to learn javascript, so please bear with me. If functions are just another class of objects, why can't I create new properties or methods for a user-defined function and reference them using the dot (.) operator. For example, if I have function defined as:

function test(tagvalue)
{
var elem = document.querySelectorAll('[data-mytag=' + tagvalue + ']')[0];
this.id = elem.id;
}

Why can't I simply call the function as: var x = test('abc').id;

  • (a) `test('abc').id` tries to read the `id` property of the return value you get when calling the function, not to the function. (b) `this` does not refer to the return value of the function (c) `this` does not refer to the function either. See the duplicate for how `this` works. – Quentin Aug 04 '17 at 15:20
  • Thank you Quentin. I will review that in detail. What would you suggest is the most appropriate way to accomplish what I am attempting? – Bob Richards Aug 04 '17 at 16:04

0 Answers0