简体   繁体   English

Value 及其原型的比较逻辑

[英]Comparison logic on Value and it's Prototype

So, there is a " problem ":所以,有一个“问题”:
You have你有

let kind_1_ = "some_string"
let kind_2_ = 1234
let kind_3_ = [ "can be string or...", 0, { "some_prop" : "any possible type of value" }, ["...or this"] ]
let kind_4_ = { "even_this" : "will be appropriate" }

const prototype = [ "string", 0, [], {} ]
/* as well as   = [ "string", 0, [] ]       */
/* or as        = [ 0, {} ]                 */
/* and so on... but in most cases with this
   | 4 particular types                     */

So, we have to compare kind s to the prototype .因此,我们必须将kind原型进行比较。 There is only 1 kind and 1 prototype given at the time.当时只给出了1种1个原型 My code for this was as simple as this:我的代码就像这样简单:

let this_will_be_called = (kind, proto) => {
    let bool_to_return = false
    for (let each in proto)
    {
        if (typeof(kind) == typeof(proto)) {
            bool_to_return = true
            return bool_to_return
        }
    }
    return bool_to_return
}

this_will_be_called(kind_any_of_kinds, prototype)

Of course, it's not working the way it should.当然,它没有按照它应该的方式工作。 One Major problem is that you can't effectively compare [] to {}, because they are both of type Object ( I know about Array.isArray() ).一个主要问题是您无法有效地将 [] 与 {} 进行比较,因为它们都是Object类型(我知道 Array.isArray() )。 But in case of the following situations, it must return false :但在以下情况下,它必须返回false

let kind = []
var prototype = [.., {}] /* without [] in here */

/* or this */
let kind = {}
var prototype = [.., []] /* without {} in here */

/* but must return true if: */
let kind = {} /* or let kind = [] */
var prototype = [.., [], {}]

And in the end, it must return false if no types matching.最后,如果没有类型匹配,它必须返回false
My logic is explained in this pseudo-code :这个伪代码解释了我的逻辑:

FOR every element in prototype:
    IF (type of element == type of kind) and (element is NOT Array):
        return TRUE
    ELSE IF (type of element == type of kind) and (element is Array):
        IF (type of kind is NOT an Object) and (kind is Array):
            return TRUE
        ELSE IF (type of kind is an Object):
            return FALSE
    ELSE IF (type of element == type of kind) and (element is Object) and (element is NOT an Array):
        IF (kind is NOT an Array):
            return TRUE
        ELSE IF (kind is an Array):
            return FALSE
    ELSE:
        return FALSE

Now, I need your attention to audit my logic, because I've completely broke my brain on solving this.现在,我需要你的注意力来审核我的逻辑,因为我在解决这个问题时已经完全脑残了。 Even if above logic works, it's not an elegant solution.即使上述逻辑有效,也不是一个优雅的解决方案。 So, you could modify it for better performance.因此,您可以对其进行修改以获得更好的性能。

My appreciation on every solution!我对每个解决方案的赞赏! ;) ;)

First thing first, your prototype is an array, so when you do for..in you get an index of the array, not the item, so you'll need add something like each = proto[each];首先,您的prototype是一个数组,因此当您执行for..in您将获得数组的索引,而不是项目,因此您需要添加类似each = proto[each];

Than, when it comes to detect array vs object, you can detect if kind or each an array compare them and return if the same, if none is an array, proceed as before with typeof .然后,当涉及到检测数组与对象时,您可以检测kindeach数组是否比较它们,如果相同则返回,如果没有一个是数组,则像以前一样继续typeof

 let this_will_be_called = (kind, proto) => { let bool_to_return = false for (let each in proto) { each = proto[each]; const isKindArray = Array.isArray(kind), isEachArray = Array.isArray(each); if (isKindArray || isEachArray) { if (isKindArray && isEachArray) return true; } else if (typeof(kind) == typeof(each)) { bool_to_return = true return bool_to_return } } return bool_to_return } let kind = [] let prototype = [ "string", 0, {} ] /* without [] in here */ console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype) /* or this */ kind = {} prototype = [ "string", 0, [] ] /* without {} in here */ console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype) /* but must return true if: */ kind = {} /* or let kind = [] */ prototype = [ "string", 0, [], {} ] console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype) /* as well as = [ "string", 0, [] ] */ /* or as = [ 0, {} ] */ /* and so on... but in most cases with this | 4 particular types */ kind= "some_string" console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype) kind = 1234 console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype) kind = [ "can be string or...", 0, { "some_prop" : "any possible type of value" }, ["...or this"] ] console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype) kind = { "even_this" : "will be appropriate" } console.log(this_will_be_called(kind, prototype), "kind", kind, "proto", prototype)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM