<body>
1.对象概念?
Object-对象
2.如何创建对象?
let obj={
属性:属性值, --键值对(逗号隔开)
属性2:属性值2
}
<script>
/*字面量方式创建*/
let student={
//属性(名词)
name:"月月",
className:"3",
shenGao:"160",
//行为(动词)
eating:function () {
console.log("吃饭")
}
}
/*new关键词创建*/
let obj=new Object();
obj.name="月月";
obj.age="24";
obj.run=function () {
console.log("跑步")
}
obj.run();
</script>
3.对象中最重要的思想(面向对象)
面向过程:强调的是事件的流程和顺序
面向对象:强调的是角色、主体
4.对象的特性
封装、继承、多态、抽象(封装和抽象是现阶段需要掌握的)
封装:将某个方法写成多用的
getNode("div")
function getNode(Nodes){
return document.getElementById(Nodes);
}
抽象:关注它的某些属性,把它抽取出来;作为共同的特性进行处理
<script>
/*模板 通过模板创建对象*/
function Student(stuId,stuName,stuSex,stuAge) {
//属性 this指向实例化对象
this.stuId=stuId;
this.stuName=stuName;
this.stuSex=stuSex;
this.stuAge=stuAge;
this.study=function () {
console.log("我爱学习代码!!!")
}
}
let myStudent=new Student(1,"月月","女",18);
console.log(myStudent.stuName)
myStudent.study();
let myStudent2=new Student(2,"博文","男",28);
console.log(myStudent2)
/*let student1={
id:"001",
name:"月月",
sex:"女",
age:"24",
study:function () {
console.log("我爱学习代码!!!")
}
}
let student2={
id:"002",
name:"博文",
sex:"男",
age:"28",
study:function () {
console.log("我爱学习代码!!!")
}
}*/
/*let myArr=new Array();
function Array() {
this.push=function () {
};
this.pop=function () {
}
}
myArr.push();*/
function BoyFriend(height,handsome,money) {
this.height=height;
this.handsome=handsome;
this.money=money;
this.maimaimia=function () {
this.money=this.money-5000;
console.log("我现在还有:"+this.money)
}
}
// let bf= new BoyFriend(180,true,parseInt(Math.random()*50000+100))
// console.log(bf);
// bf.maimaimia();
/*创建多个*/
let bfArray=new Array(); //创建好了放数组里
function creatBf() {
let num=parseInt(Math.random()*10)
if(num>5){
handsome=true
}else{
handsome=false
}
let bf= new BoyFriend(num,handsome,parseInt(Math.random()*50000+100))
//放到数组中
bfArray.push(bf);
console.log(bfArray)
}
// creatBf();
//每隔3秒创建一个
setInterval(creatBf,2000);
</script>
保留小数:toFixed()方法,括号内写保留小数的位数
</body>
【JS】什么是对象
于 2024-03-24 16:04:58 首次发布