gpt4 book ai didi

javascript - 如何通过原型(prototype)传递多维数组?

转载 作者:行者123 更新时间:2023-11-30 09:30:28 24 4
gpt4 key购买 nike

问题

每当我调用 arrayGuide 时,我都会得到一个error

Array.prototype.arrayGuide = function() {
console.log(this) // When called… this should be getting logged.
}
//How to get this prototype to work


replace = {
basic: {
stage1: {
one: ["hello", "world"],
two: ["brother", "sister"],
three: ["baby", "adult"]
},
stage2: {
one: ["1"],
two: ["2"],
three: ["3"]
}
},
advanced: {
humans: [/^biology\s/gi, /science$/i]
}
}

replace.arrayGuide() // This keeps throwing an error message
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>



我应该得到的结果

我应该从第一个演示中得到这些结果。但出于某种原因,我不是。

function arrayRegExpCreator(place) {
console.log(place) // I should be getting this…
}

replace = {
basic: {
stage1: {
one: ["hello", "world"],
two: ["brother", "sister"],
three: ["baby", "adult"]
},
stage2: {
one: ["1"],
two: ["2"],
three: ["3"]
}
},
advanced: {
humans: [/^biology\s/gi, /science$/i]
}
}

arrayRegExpCreator(replace)

最佳答案

第一个片段是扩展 Array 原型(prototype)而不是 Object。由于 replaceObject 的一个实例,您应该这样做:

Object.prototype.arrayGuide = function() {
console.log(this) // When called… this should be getting logged.
}

Object.prototype.arrayGuide = function() {
console.log(this) // When called… this should be getting logged.
}
//How to get this prototype to work


replace = {
basic: {
stage1: {
one: ["hello", "world"],
two: ["brother", "sister"],
three: ["baby", "adult"]
},
stage2: {
one: ["1"],
two: ["2"],
three: ["3"]
}
},
advanced: {
humans: [/^biology\s/gi, /science$/i]
}
}

replace.arrayGuide() // This keeps throwing an error message

关于javascript - 如何通过原型(prototype)传递多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46601218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com