Skip to main content

Bug Tracker

Side navigation

#9186 closed bug (wontfix)

Opened May 09, 2011 09:12AM UTC

Closed May 09, 2011 01:38PM UTC

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

Attachments (0)
Change History (1)

Changed May 09, 2011 01:38PM UTC by dmethvin comment:1

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.