构造函数和class函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
</html>
<script>
function People(name,age){
this.name = name;
this.age = age;
}
People.info = 'bb' ;//直接挂载在构造函数上,所以是静态属性
People.prototype.say=function(){
console.log('这是People实例属性')
}
let p1 = new People('王少',22);
console.log(p1);
console.log(p1.name);//通过new出来的实例,访问到的属性叫做实例属性;
console.log(p1.age);
p1.say();//这是实例的方法
console.log(People.info)//静态属性:通过构造函数,直接访问的属性,叫做静态属性。
</script>
————————————————————————————————————————————————————————————————
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
</html>
<script>
//创建个动物类
class Animal{
//这是 类的构造器 构造器的作用:每当new这个类的时候,必然会先执行构造器的代码
constructor(name,age){
this.name=name;
this.age=age;
}
static info = 'gender';//在class内部,通过static 修饰的属性叫做静态属性;
jion(){
console.log('这是Animal实例属性');
}
static jiao(){
console.log('这是Animal静态方法');//在class内部,通过static 修饰的属性叫做静态方法;
}
}
const dog = new Animal('大黄',2);
console.log(dog);
console.log(dog.name);
console.log(dog.age);
dog.jion();
console.log(Animal.info);
Animal.jiao();
</script>