Javascript Koans-无法解决一项测试 [英] Javascript Koans - cannot solve one test

查看:39
本文介绍了Javascript Koans-无法解决一项测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决Javascript Koans的测试问题,并卡在反射"块上.任何人都可以解决和解释下一个步骤的解决方案:

  test("hasOwnProperty",function(){//如果参数是直接在对象上的属性,则hasOwnProperty返回true,//,但如果它是可以通过原型链访问的属性,则不是.var keys = [];varfruits = ['apple','orange'];for(水果中的propertyName){keys.push(propertyName);}ok(keys.equalTo(['__','__','__']),'数组的属性是什么?');var ownKeys = [];for(水果中的propertyName){如果(fruits.hasOwnProperty(propertyName)){ownKeys.push(propertyName);}}ok(ownKeys.equalTo(['__','__']),'数组本身的属性是什么?');}); 

解决方案

您必须知道JavaScript具有原型继承模型,而不是经典模型.实际上,这意味着继承是通过所谓的原型链"完成的.

当您尝试使用"for in"访问数组的属性时,它将从原型链一直爬升"到arrayname.prototype,因为它继承自它!

如果您想了解更多有关此功能的信息,强烈建议您查看 Ivo Wetzel的> JavaScript Garden ,这是我最初找到答案的地方!

对于其他情况,您还应该知道javascript将数组的自有"属性引用为十进制数字,即:第一个属性为'0',第二个属性为'1',等等.

因此,解决方案是这样的:

  test("hasOwnProperty",function(){//如果参数是直接在对象上的属性,则hasOwnProperty返回true,//,但如果它是可以通过原型链访问的属性,则不是.var keys = [];varfruits = ['apple','orange'];for(水果中的propertyName){keys.push(propertyName);}ok(keys.equalTo(['0','1','fruits.prototype'),'数组的属性是什么?');var ownKeys = [];for(水果中的propertyName){如果(fruits.hasOwnProperty(propertyName)){ownKeys.push(propertyName);}}ok(ownKeys.equalTo(['0','1']),'数组本身的属性是什么?'); 

});

I'm trying to solve test from Javascript Koans and stuck on "reflection" block. Could anyone solve and explain solution for next block:

    test("hasOwnProperty", function() {
    // hasOwnProperty returns true if the parameter is a property directly on the object, 
    // but not if it is a property accessible via the prototype chain.    
    var keys = [];
    var fruits =  ['apple', 'orange'];
    for(propertyName in fruits) {
        keys.push(propertyName);
    }
    ok(keys.equalTo(['__','__', '__']), 'what are the properties of the array?');

    var ownKeys = [];
    for(propertyName in fruits) {
        if (fruits.hasOwnProperty(propertyName)) {
            ownKeys.push(propertyName);
        }
    }
    ok(ownKeys.equalTo(['__', '__']), 'what are the own properties of the array?');
    });

解决方案

You must know that JavaScript features a prototypal inheritance model instead of a classical one. In fact, what this means is that inheritance is done through what is called prototype chains.

When you try to access an array's properties with a "for in", it will "climb up" the prototype chain all the way to arrayname.prototype, since it inherits from it !

If you want to learn more about this feature, I strongly suggest you take a look at JavaScript Garden from Ivo Wetzel, which is where I initially found my answers !

For the rest, you should also know that javascript refers to an array's "own" properties as decimal numbers, ie.: First Property is '0', Second is '1', etc.

So, solution goes like this :

test("hasOwnProperty", function() {
// hasOwnProperty returns true if the parameter is a property directly on the object, 
// but not if it is a property accessible via the prototype chain.
var keys = [];
var fruits =  ['apple', 'orange'];
for(propertyName in fruits) {
    keys.push(propertyName);
}
ok(keys.equalTo(['0', '1', 'fruits.prototype'), 'what are the properties of the array?');

var ownKeys = [];
for(propertyName in fruits) {
    if (fruits.hasOwnProperty(propertyName)) {
        ownKeys.push(propertyName);
    }
}
ok(ownKeys.equalTo(['0', '1']), 'what are the own properties of the array?');

});

这篇关于Javascript Koans-无法解决一项测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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