欢迎光临散文网 会员登陆 & 注册

JS(改进版):对象,构造方法的作用为给对象赋值(传值等),随机色块

2020-06-16 00:10 作者:诗书画唱  | 我要投稿

创建一个人类对象,包含人物编号,名称,年龄,性别属性,包含一个吃饭的功能,将该对象打印到主界面中




var person={


'bianhao':007,


'name':'诗书画唱',


'age':20,


'sex':'男',


'chifan' :function(){


document . write(this .name+"能够吃饭");}


};




document . write("编号:"+person. bianhao+

";  "+"姓名:"+person. name +

";  "+"年龄:"+person. age+" ;   "

+"性别:"+person. sex) ;

document . write("<br />");

document . write("&nbsp;&nbsp;"+"功能:");person. chifan();






创建一个人类的对象数组,用来保存3名人员信息,遍历人员信息


<!DOCTYPE html>


<html>


<head>


<meta charset="utf-8" />


<title></title>


<script>


var persons=[


{'bianhao':666,'name':'给诗书画唱','age':20},


{'bianhao':888,'name':'关注','age':21},


{'bianhao':233,'name':'三连','age':22},


];


for(var i of persons){


for(var j in i){


document . write(i[j]+"&nbsp;");}


document . write("<br>");}


</script>


</head>


<body>


</body>


</html>






创建商品的构造方法,实例化3件商品放入到对象数组中,计算商品的总价格


<!DOCTYPE html>


<html>




<head>


<meta charset="utf-8" />


<title></title>


<script>


var shangPin = function(bianhao, name, price, num) {


this.bianhao = bianhao;


this.name = name;


this.price = price;


this.num = num;


}


var shangPinShuZu = [


new shangPin(1,'三连', 6, 6),


new shangPin(2,'关注',6, 6),


new shangPin(3, '诗书画唱', 6, 6)


];


alert("计算总价格为:"+jiSuanZongJiaGe(shangPinShuZu)+"元");




function jiSuanZongJiaGe(shangPinShuZu) {


var zongJiaGe = 0; 


for(var i of shangPinShuZu) {


var meiGeShangPinJiaGe = (i.price * i.num);


zongJiaGe += meiGeShangPinJiaGe;


}


return zongJiaGe;




}


</script>


</head>




<body>




</body>




</html>




随机色块


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<script>


var colors = ["red", "blue", "green", "yellow"];

var fangkuai=function(width, height, color, top, left) {

this .width=width;

this.height = height;

this.color = color;

this.top = top;

this.left= left;

}

var fangkuais = [

new fangkuai("30px", "30px", colors [Math. floor(Math.random() * colors. length)],

Math. floor(Math. random() * 500), Math. floor(Math. random()*500)),



new fangkuai("30px", "30px", colors [Math. floor(Math.random() * colors. length)],

Math. floor(Math. random() * 500), Math. floor(Math. random()*500))


];

function dedaodiv() {

var div1 = document . getElementById("div1");

for(var j of fangkuais) {

var creatediv = document. createElement("div");

creatediv.style.width = j .width;

creatediv.style.height=j. height;

creatediv. style. backgroundColor = j.color;

creatediv.style.top = j.top + "px" ;

creatediv.style.left = j.left + "px";

creatediv. style.position="absolute";

div1. appendChild(creatediv);

console.log(creatediv);

}}

window.onload = function() {

dedaodiv();}

</script>

<body><div id="div1" style="width: 530px; height: 530px;

position: relative; margin: 0px auto; background-color: gray;">

</div>

</body>

</html>


JS(改进版):对象,构造方法的作用为给对象赋值(传值等),随机色块的评论 (共 条)

分享到微博请遵守国家法律