更新时间:2019年07月26日 11时16分00秒 来源:黑马程序员论坛
面向对象思想 什么是面向对象编程(Object Oriented Programming| OOP)?
JS中并没有类(class)的概念,更多的时候我们称之为对象(function),然后把对象叫做实例(instance)。如下,Person 类型为function class Person {// ... sayHi() { console.log('Hi') } } typeof Person // 'function' Person === Person.prototype.constructor // 'true' let p1 = new Person(); p1.sayHi() // Hi 上列代码表明,类的数据类型就是函数,类本身就是指向构造函数。 使用的时候,也是直接对类使用new命令,跟构造函数的用法完全一致。 类的由来在js里面,我们通常都是通过构造函数来创建对象(class),然后通过new这个关键字来实例化一个对象,如: ES5中的方法:
构造函数的prototype属性 构造函数的prototype属性,在 ES6 的“类”上面继续存在。事实上,类的所有方法都定义在类的prototype属性上面。
showName(name) { return `我的名字为:${name}` } } let p1 = new Person(); console.log(p1.showName('Jing')) 类的传参实例: class Person { constructor(name,age) { this.name = name; this.age = age; } showName() { return `我的名字为:${this.name}` } showAge() { return `我的年龄为:${this.age}` } } let p1 = new Person('Jing',18) console.log(p1.showName()); // 我的名字为:Jing console.log(p1.showAge()); // 我的年龄为:18 class Person { } // 等同于 class Person { constructor () {} } 表达式形式 let Person = class { constructor(name,age) { this.name = name; this.age = age; } showName() { return `我的名字为:${this.name}` } showAge() { return `我的年龄为:${this.age}` } } let p1 = new Person('Jing',18) console.log(p1.showName()); // 我的名字为:Jing console.log(p1.showAge()); // 我的年龄为:18
constructor(name,age) { this.name = name; this.age = age; } showName() { return `我的名字为:${this.name}` } }('Jing') console.log(Person.showName()) // Jing属性表达式: 类的属性名可以采用表达式let methodName = 'showName'; class Person { constructor (name) { this.name = name; } [methodName]() { return `我的名字为:${this.name}` } } let p1 = new Person('Jing') console.log(p1.showName()); // 我的名字为:Jing // 字符串拼接形式 let aa = 'show'; let bb = 'Name'; class Person { constructor (name) { this.name = name; } [aa+bb]() { return `我的名字为:${this.name}` } } let p1 = new Person('Jing') console.log(p1.showName()); // 我的名字为:Jingclass取值函数,存值函数 class Person { constructor() { } get aaa() { return `aaa的属性` } set aaa(val) { console.log(`设置aaa属性,值为:${val}`); } } let p1 = new Person(); p1.aaa = '123' p1.aaa //设置aaa属性,值为:123 注意点严格模式 不存在变量提升 let p1 = new Person() console.log(p1); class Person { constructor (){ this.name = 'Jing' } } // 报错显示初始化前无法访问Person(即Person类没有创建)静态方法 class Person { constructor() { } showName() { return `这是showName方法` } static aaa() { return `这是静态方法` } } let p1 = new Person(); console.log(p1.showName()); //这是showName方法 console.log(Person.aaa()); // 这是静态方法实例属性新写法 class IncreasingCounter { constructor() { this._count = 0; } get value() { console.log('Getting the current value!'); return this._count; } increment() { this._count++; }} class IncreasingCounter { _count = 0; get value() { console.log('Getting the current value!'); return this._count; } increment() { this._count++; }} 类的继承 class Person { constructor(name) { this.name = name; } showName() { return `我的名字为${this.name}` } } // 子类 class Student extends Person{ } // 调用 let stu1 = new Student('Jing') console.log(stu1.showName()); super class Person { constructor(name) { this.name = name; } showName() { return `我的名字为${this.name}` } } // 子类 class Student extends Person{ constructor(name,skill) { super(name) this.skill = skill } } // 调用 let stu1 = new Student('Jing','学习') console.log(stu1.skill); |
推荐了解热门学科
java培训 | Python人工智能 | Web前端培训 | PHP培训 |
区块链培训 | 影视制作培训 | C++培训 | 产品经理培训 |
UI设计培训 | 新媒体培训 | 产品经理培训 | Linux运维 |
大数据培训 | 智能机器人软件开发 |
传智播客是一家致力于培养高素质软件开发人才的科技公司,“黑马程序员”是传智播客旗下高端IT教育品牌。自“黑马程序员”成立以来,教学研发团队一直致力于打造精品课程资源,不断在产、学、研3个层面创新自己的执教理念与教学方针,并集中“黑马程序员”的优势力量,针对性地出版了计算机系列教材50多册,制作教学视频数+套,发表各类技术文章数百篇。
传智播客从未停止思考
传智播客副总裁毕向东在2019IT培训行业变革大会提到,“传智播客意识到企业的用人需求已经从初级程序员升级到中高级程序员,具备多领域、多行业项目经验的人才成为企业用人的首选。”
中级程序员和初级程序员的差别在哪里?
项目经验。毕向东表示,“中级程序员和初级程序员最大的差别在于中级程序员比初级程序员多了三四年的工作经验,从而多出了更多的项目经验。“为此,传智播客研究院引进曾在知名IT企业如阿里、IBM就职的高级技术专家,集中研发面向中高级程序员的课程,用以满足企业用人需求,尽快补全IT行业所需的人才缺口。
何为中高级程序员课程?
传智播客进行了定义。中高级程序员课程,是在当前主流的初级程序员课程的基础上,增加多领域多行业的含金量项目,从技术的广度和深度上进行拓展。“我们希望用5年的时间,打造上百个高含金量的项目,覆盖主流的32个行业。”传智播客课程研发总监于洋表示。
黑马程序员热门视频教程【点击播放】
Python入门教程完整版(懂中文就能学会) | 零起点打开Java世界的大门 |
C++| 匠心之作 从0到1入门学编程 | PHP|零基础入门开发者编程核心技术 |
Web前端入门教程_Web前端html+css+JavaScript | 软件测试入门到精通 |