如何检查变量是对象还是数组?

时间:2018-11-10 03:49:34

标签: javascript arrays object

我有一个我不知道如何解决的问题,我测试了一些有关如何比较或检查变量是<!DOCTYPE html> <html> <body> <style> canvas { background: #eee; } </style> <canvas id="ctx" tabindex=0 width=900 height=500 style="border:1px solid #000000;" onkeypress="movePlayer(event)" onkeyup="keyUp(event)"></canvas> <script> var canvas = document.getElementById("ctx"); var ctx = canvas.getContext("2d"); canvas.setAttribute('tabindex', 0); canvas.focus(); canvas.addEventListener("keydown", movePlayer); //Maybe I can get a class working? class Platform { constructor(x, y, xS, yS, moveBool) { this.xPos = x; this.yPos = y; this.xSize = xS; this.ySize = yS; this.moveable = moveBool; if (this.moveable) { this.setDirection('right') } } render() { ctx.save() ctx.fillStyle = "red"; ctx.fillRect(this.xPos, this.yPos, this.xSize, this.ySize); ctx.restore() } get getX() { return this.xPos; } get getY() { return this.yPos; } get getxSize() { return this.xSize; } get getySize() { return this.ySize; } get getMoveable() { return this.moveable; } setDirection(direction) { this.direction = direction } moveX(speed) { this.xPos += speed; } } //Platform array: platformArray = []; //Platforms: platform1 = new Platform(100, 350, 200, 10, true); platformArray.push(platform1); platform2 = new Platform(300, 200, 200, 10, false); platformArray.push(platform2); platform3 = new Platform(400, 300, 200, 10, false); platformArray.push(platform3); //Vars: var x_new = 50; var y_new = 50; var isJumping = false; var isColliding = false; var speed = 10; var keys = { up: false, right: false, left: false }; function movePlayer(event) { switch (event.keyCode) { //Right key down: case 39: keys["right"] = true; break; //Left key down: case 37: keys["left"] = true; break; //Up key down: case 38: keys["up"] = true; isJumping = true; break; } } function keyUp(event) { switch (event.keyCode) { //Up key up: case 38: isJumping = false; keys["up"] = false; break; //Right key up: case 39: keys["right"] = false; break; //Left key up: case 37: keys["left"] = false; break; } } function boundsIntersect(x1, y1, x2, y2) { if (x1 > x2 - 50 && x1 < x2 + 200 && y1 < y2 && y1 > y2 - 55) { return true; } return false; } function update() { ctx.clearRect(0, 0, 900, 500); //Move platforms: platformArray.forEach(function(platform) { if (platform.getMoveable) { if (platform.getX > 0 && platform.getX < 400) { if (platform.direction === 'right') { platform.moveX(5); } else { platform.moveX(-5); } } else if (platform.getX >= 400) { platform.moveX(-5); platform.setDirection('left') } else if (platform.getX <= 0) { platform.moveX(5); platform.setDirection('right') } } }); //PLayer movement: if (keys["up"] && !keys["right"] && !keys["left"]) { y_new -= speed; } else if (keys["right"] && !keys["up"]) { x_new += speed; } else if (keys["left"] && !keys["up"]) { x_new -= speed; } else if (keys["up"] && keys["right"]) { y_new -= speed; x_new += speed; } else if (keys["up"] && keys["left"]) { y_new -= speed; x_new -= speed; } //Platform intersections: platformArray.forEach(function(platform) { if (boundsIntersect(x_new, y_new, platform.getX, platform.getY) && isJumping == false) { isColliding = true; y_new -= 10; } else if (boundsIntersect(x_new, y_new, platform.getX, platform.getY) && isJumping == true) { isJumping = false; y_new += 11; isColliding = true; } else { isColliding = false; } }); if (y_new < 440 && isJumping == false && isColliding == false) { y_new += speed; } // render platforms platformArray.forEach(function (platform) { platform.render() }) // render player ctx.save() ctx.fillStyle = "black"; ctx.beginPath(); ctx.fillRect(x_new, y_new, 50, 50); //Draw ground: ctx.beginPath(); ctx.rect(0, 490, 900, 10); ctx.fillStyle = "green"; ctx.fill(); ctx.restore() window.requestAnimationFrame(update); } window.requestAnimationFrame(update); </script> </body> </html>还是array的信息 >

我已经尝试过了

object

问题是,我想知道变量是否确实是console.log({} == []); // return false console.log({1:"haha"} == {}); // return false console.log(["haha"] == {}); // retun false object的{​​{1}}原因typeof返回[]。 / p>

{}

object

是否有console.log(isobject({1:"haha"})) // should return true; console.log(isobject(["haha"])); // should return false; 像上面那样检查console.log(isobject({})) // should return true; console.log(isobject([])); // should return false; ? 感谢您的任何纠正。

2 个答案:

答案 0 :(得分:2)

arr = [1,2,3]

Array.isArray(arr) //应该输出true

对于我会做的对象 obj = {a:1}

Object.keys(obj).length //应该输出1

所以你可以做

Object.keys(obj).length >= 0 //如果obj是obj字面量应为true。

答案 1 :(得分:2)

这会有所帮助。

var a = [], b = {};

console.log(Object.prototype.toString.call(a).indexOf("Array")>-1);
console.log(Object.prototype.toString.call(b).indexOf("Object")>-1);

console.log(a.constructor.name == "Array");
console.log(b.constructor.name == "Object");

还有许多其他方法,但是以上方法在所有浏览器中都向后兼容。

必须参考相关问题:

Check if a value is array

Check if a value is object