如果我有一个对象:
var Edge = function() {
this.a = 0;
this.b = 0;
this.prevEdge = null;
this.nextEdge = null;
// +20 other members
}
Run Code Online (Sandbox Code Playgroud)
和一个功能:
function ProcessEdge(e)
{
// some processing and then
e = e.nextEdge;
// some processing and then return something
}
Run Code Online (Sandbox Code Playgroud)
以这种方式从其他函数调用:
var e = new Edge();
// some processing and then
var res = ProcessEdge(e);
// e should be now the same object as e.nextEdge
// but it is not, e is still e
Run Code Online (Sandbox Code Playgroud)
本e应该是现在 …
我有以下对象
{
"locations": {
"Base 1": {
"title": "This is base 1",
"Suburb 1": {
"title": "Suburb 1 in Base 1",
"Area A": {
"title": "Title for Area A",
"Street S1": {
"title": "Street S1 title"
},
"Street C4": {
"title": "Street C4 title"
},
"Street B7": {
"title": "Street B7 title"
}
},
"Another Area": {
"title": "Title for Area A",
"Street S1": {
"title": "Street S1 title"
},
"Street C4": {
"title": "Street C4 title"
},
"Street B7": …Run Code Online (Sandbox Code Playgroud) 我试图检查该条目是否存在于 AsycStore 中,如果不存在,则创建一个对象并设置它。\n并且有一个返回语句返回数据对象。
\n\n这是伪代码-
\n\nAsyncStorage.getItem("dataobject").then((do) => {\n if(do == undefined)\n {\n console.log("initializing obj + setting data");\n var dobj = "data object";\n dobj.isdirty = true;\n AsyncStorage.setItem("dataobject",dobj);\n }\n return AsyncStorage.getItem("dataobject");\n}).done();\n},\nRun Code Online (Sandbox Code Playgroud)\n\n我在控制台中收到错误 -
\n\nW/ReactNativeJS: \'Error: Attempted to assign to readonly property.\\n stack: \\n <unknown>\n\nindex.android.bun\xe2\x80\xa6:53047\\n tryCallOne \nindex.android.bun\xe2\x80\xa6:5094\\n <unknown> \nindex.android.bun\xe2\x80\xa6:5160\\n <unknown> \nindex.android.bun\xe2\x80\xa6:3399\\n callTimer \nindex.android.bun\xe2\x80\xa6:2954\\n callImmediates \nindex.android.bun\xe2\x80\xa6:3003\\n <unknown> \nindex.android.bun\xe2\x80\xa6:2523\\n guard \nindex.android.bun\xe2\x80\xa6:2452\\n __callImmediates \nindex.android.bun\xe2\x80\xa6:2523\\n <unknown> \nindex.android.bun\xe2\x80\xa6:2503\\n guard \nindex.android.bun\xe2\x80\xa6:2452\\n invokeCallbackAndReturnFlushedQueue index.android.bun\xe2\x80\xa6:2501\\n URL: http://localhost:8081/index.android.bundle?platform=android&dev=true\\n line: 53047\\n message: Attempted to assign to readonly property.\'\nRun Code Online (Sandbox Code Playgroud)\n\n我不太确定我要分配的只读属性是什么。被警告的行号是这一行 …
在function类型对象中,length属性表示类型对象所需的参数数量function。例如,在下面的可视化中,对象objectlength中的字段的值为。FunctionArray1
在上面的可视化中,lengthfield 也是objectobject 类型的成员Array.prototype,其值为0。
MDN说:
Array.prototype.length反映数组中元素的数量。
cars遵循下面的&bikes数组中的定义,
var cars = new Array("Saab", "Volvo", "BMW");
var bikes = ["Honda", "Yamaha"];
Run Code Online (Sandbox Code Playgroud)
cars.__proto__.length&bikes.__proto__.length还是0。
多个数组对象( cars, bikes) 不能共享与 中的属性相同的length属性值。lengthArray.prototype
1)
根据MDN,这是一个正确的定义吗?
2)
length如果不是,字段中的含义是什么Array.prototype?
这些功能的操作方式有什么不同吗?第一个更典型的是我在考虑构造函数时所想到的。
示例1:使用它来命名和设置属性。然后使用new创建一个新的Book对象。
function Book(name, numPages) {
this.name = name;
this.numPages = numPages;
}
var myBook = new Book('A Good Book', '500 pages');
Run Code Online (Sandbox Code Playgroud)
示例 2:使用new返回一个对象并仅调用函数本身。
function Movie(name, numMinutes) {
return { name:name, numMinutes:numMinutes };
}
var best = new Movie('Forrest Gump', '150');
var other = Movie('Gladiator', '180');
Run Code Online (Sandbox Code Playgroud)
我想我想弄清楚的是它们创建对象的方式是否不同?如果是这样,一个比另一个更好吗?是否有不同的情况下,其中一种会比另一种表现得更好?
javascript constructor javascript-objects function-constructor
我正在尝试编写一个函数,该函数查看对象数组(第一个参数)并返回包含给定对象(第二个参数)的所有键/值对的所有对象的数组。
source我下面的代码仅在对象(第二个参数)包含一个键/值对时才有效。当source对象有两个或更多键/值对时,结果不符合预期。
如何解释对象中多个键/值对source?
function findObjects(collection, source) {
var result = [];
for (i=0; i<collection.length; i++) {
for (var prop in source) {
if (collection[i].hasOwnProperty(prop) && collection[i][prop] == source[prop]) {
console.log('Collection\'s object ' + [i] + ' contains the source\'s key:value pair ' + prop + ': ' + source[prop] + '!');
result.push(collection[i]);
} else {
console.log('fail');
}
}
}
console.log('The resulting array is: ' + result);
return result;
}
findObjects([{ "a": 1, "b": 2 }, …Run Code Online (Sandbox Code Playgroud) 我必须检查对象是否未定义,但是当我这样做时
typeof myUnexistingObject.myUnexistingValue == 'undefined'
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
Uncaught ReferenceError: myUnexistingObject is not defined
Run Code Online (Sandbox Code Playgroud)
那么,如何检查未定义的对象或属性?
我正在创建一个随机报价机,它将显示来自不同哲学家的随机报价。
我有一个包含哲学家及其引号的嵌套对象的对象文字。使用 jQuery 函数和 Math.random(),如何从对象文字结构中选择随机引号?有没有更好的方法来组织数据?
我从一个 jQuery 闭包开始,它会显示一个我想使用 Math.random() 修改的指定引用。
由于我是初学者,因此正在寻找解决方案的解释。提前致谢。
示例对象字面量:
var quotes =
{
awatts: {
name: "Alan Watts",
quote: "The only way to make sense out of change is to plunge into it, move with it, and join the dance."
},
etolle: {
name: "Eckhart Tolle",
quote: "Realize deeply that the present moment is all you ever have."
},
tmckenna: {
name: "Terrence Mckenna",
quote: "“The cost of sanity in this society, is a certain level of …Run Code Online (Sandbox Code Playgroud) 此方法将从 firebase 数据库检索到的 JSON 对象存储到数组分数。
function loadScoresToArray(){
databaseRef.orderByChild("Score").on("child_added", function (snap) {
scores.push(snap.val());
});
}
Run Code Online (Sandbox Code Playgroud)
运行上面的方法并打印分数的内容
loadScoresToArray();
console.log(scores);
console.log(scores[0]);
Run Code Online (Sandbox Code Playgroud)
输出
如此处所见,对象已正确添加到分数中。
但我无法使用索引检索它们。
阅读像类似的问题这个我想这可能是因为当console.log(scores[0]);被调用时,阵列仍然是空的。它尚未被填充。这只是我的猜测。
我该如何解决这个问题?有没有办法在填充数组之前创建延迟?
想分享一个我运行的简单实验,在 Win 10 下使用 node.js v6.11.0。
目标。在内存占用方面比较数组与对象。
代码。每个功能reference,twoArrays,matrix并objects创建两个相同尺寸的阵列,含有随机数。他们以稍微不同的方式组织数据。
reference 创建两个固定大小的数组并用数字填充它们。
twoArrays 通过推送填充两个数组(因此解释器不知道最终大小)。
objects 通过推送创建一个数组,每个元素是一个包含两个数字的对象。
matrix 创建一个两行矩阵,也使用推送。
const SIZE = 5000000;
let s = [];
let q = [];
function rand () {return Math.floor(Math.random()*10)}
function reference (size = SIZE) {
s = new Array(size).fill(0).map(a => rand());
q = new Array(size).fill(0).map(a => rand());
}
function twoArrays (size = SIZE) {
s = [];
q = [];
let i = 0;
while (i++ …Run Code Online (Sandbox Code Playgroud) javascript arrays memory-management node.js javascript-objects
javascript ×10
arrays ×2
constructor ×1
ecmascript-5 ×1
jquery ×1
json ×1
node.js ×1
random ×1
react-native ×1
reactjs ×1
undefined ×1