简体   繁体   English

如何在JavaScript中的函数中访问数组对象?

[英]How to access array objects inside a Function in javascript?

I'm a beginner in javascript, I have a written a javascript program for snakes and ladders using array objects, but i'm not able to understand how to call the variable snake_pos with its start and end position. 我是javascript的初学者,我为使用数组对象的蛇和梯子编写了一个javascript程序,但是我无法理解如何使用其开始和结束位置调用变量snake_pos。 Here is the code: 这是代码:

 var player_pos = 4; var dice_value = Math.floor(Math.random() * 6) + 1; function dice() { player_pos = player_pos + dice_value; } dice(); var last_pos = 25; function game_rule() { if (player_pos >= last_pos) { alert("won the game"); } } game_rule(); function snake() { var snake_pos = [{ start_pos: 11, end_pos: 4 }, { start_pos: 20, end_pos: 7 }, { start_pos: 24, end_pos: 3 } ] } console.log(player_pos); console.log(dice_value); console.log(snake_pos); 

You have to return your variable snake_pos to use it outside the function. 您必须return变量snake_pos才能在函数外部使用它。 After that you can call the items in the array and object , take a look at this example: 之后,您可以调用arrayobject的项目,请看以下示例:

 function snake(){ // return the variable to use it outside the function return snake_pos = [ {start_pos:11, end_pos:4}, {start_pos:20, end_pos:7}, {start_pos:24, end_pos:3} ] } //store the returned value in a variable snake_pos = snake(); //log the first array item start_pos console.log(snake_pos[0].start_pos); //log the second array item start_pos console.log(snake_pos[1].start_pos); 

Simply declare snake_pos before in the same scope in which you're trying to access it: 只需在您尝试访问它的相同作用snake_pos之前声明snake_pos即可:

 var player_pos = 4; var dice_value = Math.floor(Math.random() * 6) + 1; // declare it here var snake_pos = [{ start_pos: 11, end_pos: 4 }, { start_pos: 20, end_pos: 7 }, { start_pos: 24, end_pos: 3 } ]; function dice() { player_pos = player_pos + dice_value; } dice(); var last_pos = 25; function game_rule() { if (player_pos >= last_pos) { alert("won the game"); } } game_rule(); function snake() { // It will be accessible here as well } console.log(player_pos); console.log(dice_value); // It will be accessible here console.log(snake_pos); 

Check the below code and try to understand how its work. 检查以下代码,并尝试了解其工作方式。

 // declear global variables here var player_pos = 4; var last_pos = 25; var dice_value = Math.floor(Math.random()*6)+1; // function to play dice add dice value function dice(){ dice_value = Math.floor(Math.random()*6)+1; player_pos = player_pos + dice_value; } // we have snake positions in this, we check user on snake position or not we will do action accordingly function snake(){ var snake_pos = [ {start_pos:11, end_pos:4}, {start_pos:20, end_pos:7}, {start_pos:24, end_pos:3} ]; for(var i =0; i < snake_pos.length; i++) { if(snake_pos[i].start_pos == player_pos) { player_pos = snake_pos[i].end_pos; } } } // in this we define game rule, if user won we return true function game_rule(){ if (player_pos >= last_pos) { alert("won the game"); return true; } else { return false; } } // we play game until user won this game while(game_rule() != true) { console.log(player_pos); console.log(dice_value); dice(); snake(); } 

Let me know if you have any doubt. 如果您有任何疑问,请告诉我。

Happy Coding !!! 快乐编码!

 function Snake() { this.snake_pos = [{ start_pos: 11, end_pos: 4 }, { start_pos: 20, end_pos: 7 }, { start_pos: 24, end_pos: 3 } ] } var snake = new Snake(); console.log(snake.snake_pos[0].start_pos); 

Check this: https://www.phpied.com/3-ways-to-define-a-javascript-class/ 检查以下内容: https : //www.phpied.com/3-ways-to-define-a-javascript-class/

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

相关问题 我如何访问javascript函数返回的数组中的对象 - How do i access the objects inside the array returned by a javascript function 如何访问对象函数(javascript)中的objects变量? - How can i access the objects variable inside an objects function (javascript)? 如何在javascript中访问数组/对象内部函数? - How to access array/object inside function in javascript ? 如何访问 Javascript 中的对象数组内的元素以获取 movieapp 原型 - How to access elements inside an array of objects in Javascript for a movieapp prototype 您可以访问数组内的javascript自定义类对象吗?如何访问? - Can you access javascript custom class objects inside an array and how? 如何编写返回数组内的对象作为输出的 Javascript function - How to write a Javascript function that returns objects inside an array as outputs Javascript 函数在对象数组中调用? - Javascript function calling inside an array of objects? 如何访问 Angular 中对象数组中的数组? - How to access array inside of an array of objects in Angular? Javascript:如何访问存储的数组中的当前索引值 function - Javascript: How to access the current index value inside an array stored function 如何在异步函数中访问双嵌套数组中的数据 - javascript? - How to access data inside double nested array in async function - javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM