美文网首页
javascript的面向对象编程

javascript的面向对象编程

作者: 你好667 | 来源:发表于2017-08-25 10:31 被阅读0次

原型

类似与继承关系(只是变成了对象继承对象)

prototype ==  "原型"

var Person = {
    name: 'lin',
    height: 1.8,
    run: function () {
        console.log(this.name + ' is running...');
    }
};

var linxw = {
    name: 'linxw'
};

linxw.__proto__ = Person;

linxw.name; // 'linxw'
linxw.run(); // linxw is running.

// 就像run这个方法是从person人哪里继承过来的
  1. JavaScript不区分类和实例的概念,而是通过原型(prototype)来实现面向对象编程
  2. JavaScript的原型链和Java的Class区别就在,它没有“Class”的概念,所有对象都是实例,所谓继承关系不过是把一个对象的原型指向另一个对象而已。
  3. 原型链的概念就像继承树一样。(自下而上)

创建原型继承

  1. Object.create()
// 原型对象:
var Person = {
    name: 'Lin',
    height: 1.2,
    run: function () {
        console.log(this.name + ' is running...');
    }
};

function createStudent(name) {
    // 基于Person原型创建一个新对象:
    var s = Object.create(Person);
    // 初始化新对象:
    s.name = name;
    return s;
}

var linxw = createStudent('小明');
linxw.run(); // 小明 is running...
linxw.__proto__ === Person; // true
  1. new创建基于原型对象
function Person(name) {
    this.name = name;
}

Person.prototype.hello = function () {
    console.log(this.name);
};

var lxw = new Person("linxw");
lxw.hello();//linxw

对象的构造函数

通过 new 运算,调用函数返回一个对象,改变构造函数内this指针指向对象本身。(如果this不new的话就是返回undefined,非strict模式下this.name =name不报错,因为this绑定为window,于是无意间创建了全局变量name,并且返回undefined)

function Person(name) {
    this.name = name;
    this.hello = function () {
        alert('Hello, ' + this.name + '!');
    }
}

var xiaoming = new Person('小明');
xiaoming.name; // '小明'
xiaoming.hello(); // Hello, 小明!

//原型链
// xiaoming ----> Person.prototype ----> Object.prototype ----> null

真正的原型继承(扩展类【对象】)

// 用于继承的函数
function inherits(Child, Parent) {
    var F = function () {};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
}
// 父类
function Student(props) {
    this.name = props.name || 'Unnamed';
}

Student.prototype.hello = function () {
    alert('Hello, ' + this.name + '!');
}
// 子类
function PrimaryStudent(props) {
    Student.call(this, props);
    this.grade = props.grade || 1;
}

// 实现原型继承链:
inherits(PrimaryStudent, Student);

// 绑定其他方法到PrimaryStudent原型:
PrimaryStudent.prototype.getGrade = function () {
    return this.grade;
};

class继承

function Student(name) {
    this.name = name;
}

Student.prototype.hello = function () {
    alert('Hello, ' + this.name + '!');
}


//上下文代码 等价
class Student {
    constructor(name) {
        this.name = name;
    }

    hello() {
        alert('Hello, ' + this.name + '!');
    }
}


// 继承
class PrimaryStudent extends Student {
    constructor(name, grade) {
        super(name); // 记得用super调用父类的构造方法!
        this.grade = grade;
    }

    myGrade() {
        alert('I am at grade ' + this.grade);
    }
}

相关文章

  • 构造函数与 new 命令

    JavaScript 语言具有很强的面向对象编程能力,本章介绍 JavaScript 如何进行面向对象编程。 对象...

  • JS创建对象方案(一)

    5.1 JavaScript的面向对象 JavaScript其实支持多种编程范式的,包括函数式编程和面向对象编程:...

  • Javascript面向对象编程

    阮一峰文档备忘 Javascript 面向对象编程(一):介绍封装 Javascript 面向对象编程(二):介绍...

  • JavaScript学习

    javascript面向对象 初学javascript,感觉javascript的面向对象编程还是很有意思的,在此...

  • javascript的面向对象

    javascript面向对象 初学javascript,感觉javascript的面向对象编程还是很有意思的,在此...

  • JavaScript学习笔记(一)

    Javascript面向对象 1. 面向对象编程介绍 1.1 两大编程思想 面向过程 & 面向对象 1.2 面向过...

  • javascript 面向对象编程

    引自:阮一峰的博客Javascript面向对象编程(一):封装Javascript面向对象编程(二):构造函数的继...

  • ajax

    1. 面向对象 javascript 具有面向过程,面向对象,函数式编程的特点 javascript 重要 原型/...

  • javascript面向对象编程

    javascript面向对象编程一(封装) 通俗易懂绝对干货 JS面向对象编程

  • JavaScript学习笔记(五)

    主要源于廖雪峰老师的JavaScript教程 面向对象编程 1. 简介 JavaScript的面向对象编程和大多数...

网友评论

      本文标题:javascript的面向对象编程

      本文链接:https://www.haomeiwen.com/subject/fdcudxtx.html