Javascript Array Concat无法正常工作。为什么?

时间:2022-08-25 18:13:48

So I've created this jqueryui widget. Its creates a div that I can stream errors into. The widget code looks like this:

所以我创造了这个jQueryUI的小部件。它创建一个div,我可以流错误之中。窗口小部件的代码如下所示:

$.widget('ui.miniErrorLog', {
   logStart: "<ul>",   // these next 4 elements are actually a bunch more complicated.
   logEnd:   "</ul>",
   errStart: "<li>",
   errEnd:   "</li>",
   content:  "",
   refs:     [],

   _create: function() { $(this.element).addClass( "ui-state-error" ).hide(); },

   clear: function() { 
      this.content = ""; 
      for ( var i in this.refs )
         $( this.refs[i] ).removeClass( "ui-state-error" );
      this.refs = [];
      $(this.element).empty().hide(); 
   }, 

   addError: function( msg, ref ) {
      this.content += this.errStart + msg + this.errEnd; 
      if ( ref ) {
         if ( ref instanceof Array )
            this.refs.concat( ref );
         else
            this.refs.push( ref );
         for ( var i in this.refs )
            $( this.refs[i] ).addClass( "ui-state-error" );
      }
      $(this.element).html( this.logStart + this.content + this.logEnd ).show();
   }, 

   hasError: function()
   {
      if ( this.refs.length )
         return true;
      return false;
   },
});

I can add error messages into it, and references to page elements that is will put into an error state. I use it to validate dialogs. In the "addError" method I can pass in a single id, or an array of ids, like this:

我可以添加错误信息到它,并提及是将放入错误状态页面元素。我用它来验证对话框。在“addError”方法我可以通过在一个单一的ID,或ID数组,如下所示:

$( "#registerDialogError" ).miniErrorLog( 
   'addError', 
   "Your passwords don't match.", 
   [ "#registerDialogPassword1", "#registerDialogPassword2" ] );

But when I pass in an array of id's it doesn't work. The problem is in the following lines (i think):

但是,当我传入一个id的数组时,它不起作用。问题在于以下几行(我认为):

if ( ref instanceof Array )
   this.refs.concat( ref );
else
   this.refs.push( ref );

Why doesn't that concat work. this.refs and ref are both arrays. So why doesn't the concat work?

为什么连续工作都没有。 this.refs和ref都是数组。那么为什么连续工作呢?

Bonus: am I doing anything else dumb in this widget? It's my first one.

额外奖励:我在这个小部件中做了什么其他的蠢事吗?这是我的第一个。

2 个解决方案

#1


129  

The concat method doesn't change the original array, you need to reassign it.

该CONCAT方法不改变原来的数组,你需要重新分配它。

if ( ref instanceof Array )
   this.refs = this.refs.concat( ref );
else
   this.refs.push( ref );

#2


39  

Here is the reason why:

原因如下:

Definition and Usage

定义和用法

The concat() method is used to join two or more arrays.

concat()方法用于连接两个或多个数组。

This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

此方法不会更改现有数组,但会返回一个新数组,其中包含已连接数组的值。

You need to assign the result of the concatenation back in the array that you have.

您需要拼接的结果,你必须在阵列中分配了。

#1


129  

The concat method doesn't change the original array, you need to reassign it.

该CONCAT方法不改变原来的数组,你需要重新分配它。

if ( ref instanceof Array )
   this.refs = this.refs.concat( ref );
else
   this.refs.push( ref );

#2


39  

Here is the reason why:

原因如下:

Definition and Usage

定义和用法

The concat() method is used to join two or more arrays.

concat()方法用于连接两个或多个数组。

This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

此方法不会更改现有数组,但会返回一个新数组,其中包含已连接数组的值。

You need to assign the result of the concatenation back in the array that you have.

您需要拼接的结果,你必须在阵列中分配了。