ES6--类(class)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


1.js是什么语言?

        js是一个基于面向对象设计的单线程的静态脚本语言.

解释:

        面向对象     ( 类 实例对象 继承 封装 多态 )

        基于面向对象设计  本质没有继承 类 等等语法  但是按照自己的语法特征(原型)

        单线程  代码只能一个业务一个业务的执行

        静态  var a={};  var re=a*20;

        脚本语言  > 嵌入式语言 灵活

2.class概述

  • 在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类

  • class 的本质是 function

  • 可以看作一个语法糖,让对象原型的写法更加清晰、更像面向对象编程的语法。

  • 不可重复声明

  • 类定义不会被提升,这意味着必须在访问前对类进行定义,否则就会报错

3.clss定义

        类名要大写

// 命名类
class Example {
    constructor(a) {
        this.a = a;
    }
}

 

// 匿名类
let Example = class {
    constructor(a) {
        this.a = a;
    }
}

 

js没有类 ,是原型的思想设计的类,但是学习和使用这个技术时,要用类的思想学习

4.class的主体

(1)属性:ES6的类中不能直接定义变量,变量被定义在constructor中

class People {        
    //a = 10; //SyntaxError: Unexpected token =
    constructor() {
        this.a = 100; //定义变量
    }

let p = new People();
console.log(p.a);

(2)方法:constructor 方法是类的默认方法,创建类的对象时被调用。也被称为类的构造方法(构造函数、构造器)。一个类中有且仅有一个构造方法。

class People {
    constructor() {
        console.log("我是构造方法,使用new创建对象时调用");
    }

new People(); //将执行constructor方法

原型方法:不需要使用function关键字,通过“对象.原型方法”调用。

class People {
    say(world) {
        console.log(`say ${world}`);
    }
    add(a, b) {
        console.log(a + b);
    }
}
let p = new People();
p.say("hello");         //say hello
p.add(1, 2);         //3

静态方法:使用static修饰,调用时不需要创建对象,直接通过“类名.静态方法”调用

class People {
    static sum(a, b) {
        console.log(a + b);
    }
}
People.sum(1, 2);

内部类:属于外部类的成员,必须通过“外部类.内部类”访问

程序举例:

            class Person{
				constructor(a,b,c){
					console.log(666666)

					this.x=a
					this.b=b
				}
				    //对象的属性
				life=50

				    //对象的原型对象的方法
				say1(){
					console.log(111,this.x)					
				}

				    //类的静态属性
				static life=200

				    //类的静态方法
				static say2(){
					console.log(222)					
				}
			}
			console.log(typeof(Person))
			var p1=new Person(10,20,30)

	        console.log(p1)
			console.log(Person.life)
			console.log(Person.say2)

运行结果:

5.继承

     	class Person {
				life = 1
				constructor(name) {
					this.name = name
					this.makeMoney = function() {
						console.log(this.name + "学习")
					}
				}
				run() {
					console.log(this.name + "生活")
				}
				static URL = "www"
			}
			var p1 = new Person("karen")
			var p2 = new Person("jack")
			console.log(p1, p2, Person.URL, 123456)

			class Student extends Person {
				constructor(name) {
					super(name)     //让Person类的构造函数的逻辑去帮我们构建对象空间
					this.age = 20
					this.book = "书籍"
				}
			}
			var s1 = new Student("marry")
			console.log(s1, Student.URL, 222)

 运行结果:

 程序:

     	function Person1(a,b,c){
				console.log(666666)				
				this.x=a
				this.b=b
			}
			Person1.prototype.say=function(){}
			Person1.life=1
			Person1.say2=function(){
				console.log(222)					
			}
			 var p2=new Person1(10,20,30)

 运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值