How to add a property of an object from the function itself. handler.get_elem = $elem; This piece is stuffed into the function itself, so that the call remains the same. handler(result) and handler.get_elem => $elem .

 var $elem, handler = function(result) { $('#some_elem').insertAfter($elem); $('div.img_del').on('click', function() { delUserImage($(this)); }); $elem.css('color', result); }; handler.get_elem = $elem; 

Apparently really incomprehensibly wrote. What I want: there is a certain function which accepts handler.

 function query_ajax(obj, handler){ var query = ''; $.each(obj, function (key, value){ if(query.length !== 0) query += '&'; query += key + '=' + value; }); $.ajax({ type: 'POST', url: '/resp/' + query, data: query, beforeSend: function (){ if(handler.get_elem){ handler.get_elem.css('cursor', 'progress'); handler.get_elem.attr('disabled', 'disabled'); } }, success: function(data){ var result = JSON.parse(data); handler.get_elem.css('cursor', 'auto'); handler.get_elem.removeAttr('disabled'); if(result) { handler(result); } else{ } }, error: function(){ handler.get_elem.css('cursor', 'auto'); handler.get_elem.removeAttr('disabled'); } }); } 

it refers to the property handler.get_elem.css ('cursor', 'auto'); And I do not understand how handler.get_elem = $ elem; add to handler = function (result) {here in general}

  • 2
    nothing is clear what you want to get? what object is meant? What do you understand by the static property? - Grundy
  • I understand more and more how people complicate their lives for themselves :) And on the subject, write down more precisely what you want to receive. - Vasily Barbashev
  • Re-read the question. I did not understand what you want. Functions can have properties, since these are objects in JS . Assign a value and refer to it. - user207618 2:17 pm
  • just call handler.get_elem instead of $elem - Grundy
  • So I appeal to handler.get_elem, but first you need to assign $ elem to it. It just makes me add up to handler.get_elem = $ elem; after the announcement. So I ask how to bring it inside. Function () {} - iyaki

1 answer 1

How do people write in the comments, do not complicate your life, make it simple:

 var handler = function(result, elem) { $('#some_elem').insertAfter(elem); $('div.img_del').on('click', function() { delUserImage($(this)); }); elem.css('color', result); }; handler($result, $elem); 
  • where css is called forgot $ remove - Grundy
  • @Grundy, aha, corrected - MasterAlex