第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

js局部變量變?yōu)槿譄o法獲取 num

js局部變量變?yōu)槿譄o法獲取 num

慕九州8427191 2018-10-24 14:46:05
<!DOCTYPE html><html dir="ltr"><head>? <meta charset="utf-8">? <title>貪吃蛇</title>? <style media="screen">? ? .map {? ? ? width: 800px;? ? ? height: 600px;? ? ? background-color: #ccc;? ? ? position: relative;? ? ? margin: auto;? ? }? </style></head><body>? <div class="map"></div>? <script type="text/javascript">? ? //map的這個(gè)div? ? var map = document.querySelector(".map");? ? //食物對(duì)象? ? (function() {? ? ? var elements = [];? ? ? // var num=11;? ? ? //? window.num = num;? ? ? function Food(width, height, color, x, y) {? ? ? ? this.width = width;? ? ? ? this.height = height;? ? ? ? this.color = color;? ? ? ? this.x = x;? ? ? ? this.y = y;? ? ? ? console.log(this.width);? ? ? ? console.log(window);? ? ? ? var num=11;? ? ? ? window.num = num;? ? ? };? ? ? //食物方法 初識(shí)化 在地圖? ? ? Food.prototype.init = function(map) {? ? ? ? remove();? ? ? ? //把div創(chuàng)建? ? ? ? var div = document.createElement("div");? ? ? ? //方地圖? ? ? ? map.appendChild(div);? ? ? ? //初始化div? ? ? ? div.style.position = "absolute";? ? ? ? div.style.width = this.width + "px";? ? ? ? div.style.height = this.height + "px";? ? ? ? div.style.backgroundColor = this.color;? ? ? ? this.x = parseInt(Math.random() * (map.offsetWidth / this.width)) * this.width;? ? ? ? this.y = parseInt(Math.random() * (map.offsetHeight / this.height)) * this.height;? ? ? ? div.style.left = this.x + "px";? ? ? ? div.style.top = this.y + "px";? ? ? ? elements.push(div);? ? ? ? // console.log(elements);? ? ? };? ? ? function remove() {? ? ? ? for (var i = 0; i < elements.length; i++) {? ? ? ? ? var ele = elements[i];? ? ? ? ? ele.parentNode.removeChild(ele);? ? ? ? ? elements.splice(i, 1);? ? ? ? }? ? ? }? ? ? window.Food = Food;? ? }());? ? //小蛇 自調(diào)用? ? (function() {? ? ? var elements=[];? ? ? function Snake(width, height, direction) {? ? ? ? // 方塊(每個(gè))寬高? ? ? ? this.width = width;? ? ? ? this.height = height;? ? ? ? // 身體數(shù)組? ? ? ? this.body = [{? ? ? ? ? ? x: 3,? ? ? ? ? ? y: 2,? ? ? ? ? ? color: "red"? ? ? ? ? }, //頭? ? ? ? ? {? ? ? ? ? ? x: 2,? ? ? ? ? ? y: 2,? ? ? ? ? ? color: "orange"? ? ? ? ? }, //身體? ? ? ? ? {? ? ? ? ? ? x: 1,? ? ? ? ? ? y: 2,? ? ? ? ? ? color: "orange"? ? ? ? ? } //身體? ? ? ? ];? ? ? ? //方向? ? ? ? this.direction = direction||"right";? ? ? ? //初始化小蛇? ? ? ? Snake.prototype.init = function(map) {? ? ? ? ? //刪除上一次小蛇? ? ? ? ? remove();? ? ? ? ? //每個(gè)div進(jìn)行循環(huán)()? ? ? ? ? for (var i = 0; i < this.body.length; i++) {? ? ? ? ? ? var obj = this.body[i];? ? ? ? ? ? //創(chuàng)建? ? ? ? ? ? var div = document.createElement('div');? ? ? ? ? ? //放到地圖上? ? ? ? ? ? map.appendChild(div);? ? ? ? ? ? //設(shè)置樣式? ? ? ? ? ? div.style.width = this.width + "px";? ? ? ? ? ? div.style.height = this.height + "px";? ? ? ? ? ? div.style.position = "absolute";? ? ? ? ? ? div.style.left = obj.x * this.width + "px";? ? ? ? ? ? div.style.top = obj.y * this.height + "px";? ? ? ? ? ? //背景顏色? ? ? ? ? ? div.style.backgroundColor = obj.color;? ? ? ? ? ? elements.push(div);? ? ? ? ? ? ? ? console.log(num);? ? ? ? ? }? ? ? ? }? ? ? }? ? ? //小蛇移動(dòng)? ? ? Snake.prototype.move = function(food, map) {? ? ? ? //針對(duì)身體? ? ? ? for (var i = this.body.length - 1; i > 0; i--) {? ? ? ? ? this.body[i].x = this.body[i - 1].x;? ? ? ? ? this.body[i].y = this.body[i - 1].y;? ? ? ? }? ? ? ? //對(duì)于頭部? ? ? ? // console.log(this.direction);? ? ? ? switch (this.direction) {? ? ? ? ? case "right":? ? ? ? ? this.body[0].x += 1? ? ? ? ? break;? ? ? ? ? case "left":? ? ? ? ? ? this.body[0].x -= 1? ? ? ? ? ? break;? ? ? ? ? case "top":? ? ? ? ? ? this.body[0].y += 1? ? ? ? ? ? break;? ? ? ? ? case "bottom":? ? ? ? ? ? this.body[0].y -= 1? ? ? ? ? ? break;? ? ? ? ? // default:"請(qǐng)使用方向鍵進(jìn)行移動(dòng)"? ? ? ? }? ? ? }? ? ? function remove() {? ? ? ? for (var i = elements.length - 1; i >=0 ; i--) {? ? ? ? ? var ele=elements[i];? ? ? ? ? // console.log(Snake);? ? ? ? ? // console.dif(Snake);? ? ? ? ? ele.parentNode.removeChild(ele);? ? ? ? ? elements.splice(i,1);? ? ? ? }? ? ? }? ? ? window.Snake = Snake;? ? }());? ? //測(cè)試function a() {? ? ? console.log(window.num);? ? ? console.log(Food);? ? ? alert("sds")};a();? ? console.log(window);? ? // console.log(num);? ? // console.log(devicePixelRatio);? ? // console.log(Food.length);? ? var fd = new Food(20, 20, "green");? ? fd.init(map);? ? var sna = new Snake(20, 20);? ? sna.init(map);? ? sna.move(map);? ? sna.init(map);? //? ?sna.move(map);? //? ?sna.init(map);? //? ?sna.move(map);? //? ?sna.init(map);? ?</script></body></html>
查看完整描述

1 回答

已采納
?
聰明的湯姆

TA貢獻(xiàn)112條經(jīng)驗(yàn) 獲得超33個(gè)贊

你的代碼window.num = num寫在Food構(gòu)造函數(shù)里,構(gòu)造函數(shù)里面的代碼要實(shí)例化之后才會(huì)執(zhí)行,也就是new Food()之后,而你在函數(shù)a里面輸出window.num,這時(shí)候還沒成功賦值呢,應(yīng)該把a(bǔ)函數(shù)的調(diào)用放在new Food()之后。

function?Food?()?{
??window.num?=?11;
}

var?fd?=?new?Food();

function?a?()?{
??console.log(window.num);
}

a();

望采納~

查看完整回答
反對(duì) 回復(fù) 2018-10-24
  • 1 回答
  • 0 關(guān)注
  • 841 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)