Bug Tracker

Opened 9 years ago

Closed 9 years ago

#9186 closed bug (wontfix)

No way to set property by .prop() method with value which is a function

Reported by: mnaoumov Owned by:
Priority: undecided Milestone: 1.next
Component: unfiled Version: 1.6
Keywords: Cc:
Blocked by: Blocking:

Description

Sometime you need to define object where it's property names comes only in runtime.

var dynamicPropertyName = getDynamicPropertyName();
var obj = {};
obj[dynamicPropertyName] = 'value';

However it's too verbose and cannot be inlined.

jQuery 1.6 method .prop() can help

var obj = $({}).prop(dynamicPropertyName, 'value')[0];

But if value is a function this approach will not work

obj[dynamicPropertyName] = function() {alert('value');} ;

vs

var obj = $({}).prop(dynamicPropertyName, function() {alert('value');})[0];

It will not work because the function will be executed instead of just setting value.

I think there should be some parameter to disable function execution if needed.

See http://jsfiddle.net/zaTDp/1/ for more details

Change History (1)

comment:1 Changed 9 years ago by dmethvin

Resolution: wontfix
Status: newclosed

This is pretty rare. You can either assign the property to the DOM element directly as you did above, or have your function argument to .prop return a function.

Note: See TracTickets for help on using tickets.