首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ JavaScript / 文章详情

JavaScript中的面向对象

Sonder
2020-02-24
3743字
9分钟
浏览 (3.5k)

1.构造函数模式

通过创建自定义的构造函数,来定义自定义对象类型的属性和方法。

复制代码
function cons(name,age){
this.name = name;
this.age = age;
this.getMes = function(){
 console.log(`my name is ${this.name},this year ${this.age}`);
}
}
var mesge = new cons('will',21);
mesge.getMes();

2.工厂模式

该模式抽象了创建具体对象的过程,用函数来封装以特定接口创建对象的细节

复制代码
function cons(name,age){
var obj = new Object();
obj.name = name;
obj.age = age;
obj.getMes = function(){
 console.log(`my name is ${this.name},this year ${this.age}`);
}
return obj;
}
var mesge = cons('will',21);
mesge.getMes();

3.字面量模式

字面量可以用来创建单个对象,但如果要创建多个对象,会产生大量的重复代码

复制代码
var cons = {
name: 'will',
age : 21,
getMes: function(){
 console.log(`my name is ${this.name},this year ${this.age}`);
}
}
cons.getMes();

4.原型模式

使用原型对象,可以让所有实例共享它的属性和方法

复制代码
function cons(){
cons.prototype.name = "will";
cons.prototype.age = 21;
cons.prototype.getMes = function(){
 console.log(`my name is ${this.name},this year ${this.age}`);
}
}

var mesge = new cons();
mesge.getMes();

var mesge1 = new cons();
mesge1.getMes();
console.log(mesge.sayName == mesge1.sayName);//true

5.组合模式

最常见的方式。构造函数模式用于定义实例属性,而原型模式用于定义方法和共享的属性,这种组合模式还支持向构造函数传递参数。实例对象都有自己的一份实例属性的副本,同时又共享对方法的引用,最大限度地节省了内存。该模式是目前使用最广泛、认同度最高的一种创建自定义对象的模式

复制代码
function cons(name,age){
this.name = name;
this.age = age;
this.friends = ["arr","all"];
}
cons.prototype = {
getMes : function(){
 console.log(`my name is ${this.name},this year ${this.age}`);
}
}
var mesge = new cons("will",21);
var mesge1 = new cons("jalo",21);
console.log(mesge.friends);
mesge.friends.push('wc'); //还可以操作数组哈O(∩_∩)O!
console.log(mesge.friends);
console.log(mesge1.friends);
mesge.getMes();
mesge1.getMes();
console.log(mesge.friends === mesge1.friends);
console.log(mesge.sayName === mesge1.sayName);

最后在告诉你个秘密,ES6引入了类(Class),让对象的创建、继承更加直观了

复制代码
// 定义类
class Cons{
constructor(name,age){
 this.name = name;
 this.age = age;
}
getMes(){
 console.log(`hello ${this.name} !`);
}
}
let mesge = new Cons('啦啦啦~',21);
mesge.getMes();

在上面的代码片段里,先是定义了一个Cons类,里面还有一个constructor函数,这就是构造函数。而this关键字则代表实例对象。

而继承可以通过extends关键字实现。

复制代码
class Ctrn extends Cons{
constructor(name,anu){
 super(name); //等同于super.constructor(x)
 this.anu = anu;
}
ingo(){
 console.log(`my name is ${this.name},this year ${this.anu}`);
}
}
let ster = new Ctrn('will',21);
ster.ingo();
ster.getMes();

6.在Vue-Cli中使用

构造方法:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes/constructor

文件目录

复制代码
|--- components
|------ ControlJs
|-------- CmdCenterProject.js
|-------- ControllerUserProject.js
|--- ProjectPost.vue

ProjectPost.vue

复制代码
<template>
   <div>
       <h2>{{ this.Controller.getId() }}</h2>
   </div>
</template>

<script>
 import { ControllerUserMaker } from './ControlJs/ControllerUserMaker';

 export default {
   name: 'ProjectPost',
   data () {
     return {
       Controller: undefined
     }
   },
   mounted () {
     this.Controller = new ControllerUserMaker("User")
     console.log(this.Controller.getId()) // 这里是父类的方法,因为他继承【CmdCenterMarker】,所以可以直接调用
     console.log(this.Controller.getContent()) // 这里是他自己的方法
   }
 }
</script>

<style scoped></style>

CmdCenterMarker.js

复制代码
export class CmdCenterMarker {
 constructor(data){
   this.data = data;
 }
 getId() {
   return this.data;
 }
}

ControllerUserMaker.js

复制代码
import { CmdCenterMarker } from './CmdCenterMarker';
export class ControllerUserMaker extends CmdCenterMarker {
 constructor(data) {
   super(data);
   this.UserData = data;
 }
 getContent() {
   if(this.UserData === 'Admin') {
     return "管理员";
   }
   return "普通用户";
 }
}
下一篇 / CSS 故障艺术

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)