Value 与 Prototype 的比较逻辑
Comparison logic on Value and it's Prototype
所以,有一个“问题”:
你有
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 */
因此,我们必须将 种类 与 原型 进行比较。当时给出的只有1种和1种原型。我的代码很简单:
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)
当然,它没有按应有的方式工作。一个 主要 问题是您无法有效地将 [] 与 {} 进行比较,因为它们都是 Object 类型(我知道 Array.isArray())。但如果出现以下情况,则必须 return 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 = [.., [], {}]
最后,如果没有类型匹配,它必须 return false。
伪代码:
解释了我的逻辑
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
现在,我需要你注意 审核 我的逻辑,因为我在解决这个问题时完全伤透了脑筋。即使上述逻辑有效,也不是一个优雅的解决方案。所以,你可以修改它以获得更好的性能。
我对 每个 解决方案表示赞赏! ;)
首先,你的 prototype
是一个数组,所以当你执行 for..in
时,你得到的是数组的索引,而不是项目,所以你需要添加类似 each = proto[each];
然后,当涉及到检测数组与对象时,您可以检测 kind
或 each
数组比较它们,如果 return 相同,如果 none 是一个数组,像以前一样使用 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)
所以,有一个“问题”:
你有
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 */
因此,我们必须将 种类 与 原型 进行比较。当时给出的只有1种和1种原型。我的代码很简单:
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)
当然,它没有按应有的方式工作。一个 主要 问题是您无法有效地将 [] 与 {} 进行比较,因为它们都是 Object 类型(我知道 Array.isArray())。但如果出现以下情况,则必须 return 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 = [.., [], {}]
最后,如果没有类型匹配,它必须 return false。
伪代码:
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
现在,我需要你注意 审核 我的逻辑,因为我在解决这个问题时完全伤透了脑筋。即使上述逻辑有效,也不是一个优雅的解决方案。所以,你可以修改它以获得更好的性能。
我对 每个 解决方案表示赞赏! ;)
首先,你的 prototype
是一个数组,所以当你执行 for..in
时,你得到的是数组的索引,而不是项目,所以你需要添加类似 each = proto[each];
然后,当涉及到检测数组与对象时,您可以检测 kind
或 each
数组比较它们,如果 return 相同,如果 none 是一个数组,像以前一样使用 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)