为什么不ko.observableArray.length返回底层数组长度? [英] Why doesn't ko.observableArray.length return length of underlying array?

查看:1237
本文介绍了为什么不ko.observableArray.length返回底层数组长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用淘汰赛,与 observableArray ,你必须抓住基础数组时( myArray的())的为了读出的长度。我不明白为什么 myArray.length 将不会返回底层数组的长度(而不是返回0)。这是一个bug或者是有原因我已经错过了简单?

的jsfiddle: http://jsfiddle.net/jsm11482/LJVWE/

JS /型号

  VAR数据= {
    名称:ko.observable(测试),
    项目:ko.observableArray([
        {名称:ko.observable(第一事),值:ko.observable(1)},
        {名称:ko.observable(第二件事),值:ko.observable(2)},
        {名称:ko.observable(第三件事),值:ko.observable(3)}
    ])
};ko.applyBindings(数据,document.getElementsByTagName(格)[0]);

HTML

 < D​​IV>
    < H1数据绑定=TEXT:名称>< / H1>
    < UL数据绑定=的foreach:项目>
        <李数据绑定=TEXT:值()+:+名()>< /李>
    < / UL>    < D​​IV>
        <强> data.items.length ==< / STRONG>        <! - 期待3,得到0 - >
        <跨度数据绑定=TEXT:items.length>< / SPAN>
    < / DIV>
    < D​​IV>
        <强> data.items()==长度与LT; / STRONG>        <! - 期待3,得到3 - >
        <跨度数据绑定=TEXT:项目()长>< / SPAN>
    < / DIV>
< / DIV>


这是因为纯项目观察到的实际上是一个功能,它有自己的长度属性,以指示预期参数的数目。

从<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function#Properties\"相对=nofollow> MDN对功能(属性部分)页面


  

长度结果
  指定函数预期参数的数目。


由于使用KnockoutJS库开发者我也开始期望的一个observableArray的长度是基础数组的长度,但我AP preciate的KO库不重写这个功能虽然财产,因为这可能会引起其他不必要的副作用。

Using Knockout, when working with an observableArray, you must grab the underlying array (myArray()) in order to read the length. I don't understand why myArray.length wouldn't return the underlying array's length (instead it returns 0). Is this a bug or is there reason which I've simply missed?

JSFiddle: http://jsfiddle.net/jsm11482/LJVWE/

JS/Model

var data = {
    name: ko.observable("Test"),
    items: ko.observableArray([
        { name: ko.observable("First Thing"), value: ko.observable(1) },
        { name: ko.observable("Second Thing"), value: ko.observable(2) },
        { name: ko.observable("Third Thing"), value: ko.observable(3) }
    ])
};

ko.applyBindings(data, document.getElementsByTagName("div")[0]);

HTML

<div>
    <h1 data-bind="text: name"></h1>
    <ul data-bind="foreach: items">
        <li data-bind="text: value() + ': ' + name()"></li>
    </ul>

    <div>
        <strong>data.items.length == </strong>

        <!-- Expecting 3, get 0. -->
        <span data-bind="text: items.length"></span>
    </div>
    <div>
        <strong>data.items().length == </strong>

        <!-- Expecting 3, get 3. -->
        <span data-bind="text: items().length"></span>
    </div>
</div>

解决方案

This is because the plain items observable is in fact a Function, which has its own length property to indicate the number of arguments expected.

From MDN's page on Functions (properties section):

length
Specifies the number of arguments expected by the function.

As a developer using the KnockoutJS library I'd also initially expect the length of an observableArray to be the length of the underlying array, but I do appreciate the KO library not overriding this Function property though, as that would probably cause other unwanted side-effects.

这篇关于为什么不ko.observableArray.length返回底层数组长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆