消除重复代码 re: lambda 循环参数传递

发布于 2024-12-22 09:11:16 字数 1019 浏览 2 评论 0原文

问题

数组定义了以下元素:

var equipment = new Array( "bakeware", "cookware", "kitchenware", "utensils" );

存在将这些设备类型与可编辑字段关联起来的函数:

function register_equipment( equipment ) {
  $('#recipe-equipment-' + equipment).append(
    '<span class="edit-recipe-equipment"></span><br />' );

  $('.edit-recipe-equipment').editable( 'php/save.php', {
    onblur   : 'submit',
    style    : 'display: inline',
    maxlength: 15,
    size     : 10
  });

  return true;
}

这​​允许用户单击该字段并编辑其值。当用户完成编辑时,输入字段消失。

以下代码重复四次(每个设备项目一次):

  $('#equipment-new-bakeware').click( function() {
    return register_equipment( 'bakeware' );
  });

这可行,但并不理想。

问题

如何编写代码才能不重复?

以下不起作用,但显示了删除重复的意图:

for( var i = 0; i < equipment.length; i++ ) {
  $('#equipment-new-' + equipment[i]).click( function() {
    return register_equipment( equipment[i] );
  });
}

谢谢!

Problem

An array defines these elements:

var equipment = new Array( "bakeware", "cookware", "kitchenware", "utensils" );

A function exists that associates these equipment types with editable fields:

function register_equipment( equipment ) {
  $('#recipe-equipment-' + equipment).append(
    '<span class="edit-recipe-equipment"></span><br />' );

  $('.edit-recipe-equipment').editable( 'php/save.php', {
    onblur   : 'submit',
    style    : 'display: inline',
    maxlength: 15,
    size     : 10
  });

  return true;
}

This allows users to click on the field and edit its value. The input field disappears when the user finishes editing.

The following code is duplicated four times (once per equipment item):

  $('#equipment-new-bakeware').click( function() {
    return register_equipment( 'bakeware' );
  });

This works, but it is not ideal.

Question

How would you write the code such that there is no duplication?

The following does not work, but shows the intent to remove the duplication:

for( var i = 0; i < equipment.length; i++ ) {
  $('#equipment-new-' + equipment[i]).click( function() {
    return register_equipment( equipment[i] );
  });
}

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

大海や 2024-12-29 09:11:16

快速答案是创建另一个范围来捕获正确的值。有点啰嗦,但这就是想法。

function createOnClick (i) {
  // i will be captured in this function and used below
  return function () {
    register_equipment( equipment[i] );
  };
}

for( var i = 0; i < equipment.length; i++ ) {
  $('#equipment-new-' + equipment[i]).click(createOnClick(i));
}

Quick answer is to create another scope to capture the correct value. Bit long winded, but this is the idea.

function createOnClick (i) {
  // i will be captured in this function and used below
  return function () {
    register_equipment( equipment[i] );
  };
}

for( var i = 0; i < equipment.length; i++ ) {
  $('#equipment-new-' + equipment[i]).click(createOnClick(i));
}
话少情深 2024-12-29 09:11:16

从单击对象的 id 中检索设备类型并使用:

var registerEquipment = function() {
    var equipmentType = $(this).attr('id').replace('equipment-new-', '');
    return register_equipment(equipmentType);
  };

for( var i = 0; i < equipment.length; i++ ) { 
  $('#equipment-new-' + equipment[i]).click(registerEquipment);
}

Retrieve the equipment type from the id of the clicked object and use that:

var registerEquipment = function() {
    var equipmentType = $(this).attr('id').replace('equipment-new-', '');
    return register_equipment(equipmentType);
  };

for( var i = 0; i < equipment.length; i++ ) { 
  $('#equipment-new-' + equipment[i]).click(registerEquipment);
}
似最初 2024-12-29 09:11:16
for( item in equipment ) { 
  $('#equipment-new-' + item).click = function () {
    register_equipment(item);
  }
}

尝试一下;)

for( item in equipment ) { 
  $('#equipment-new-' + item).click = function () {
    register_equipment(item);
  }
}

Give it a try ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文