资讯专栏INFORMATION COLUMN

JavaScript面向对象知识点小结

rubyshen / 1224人阅读

摘要:面向对象主要知识点小结,基于构造函数可以理解为通过即将创建的对象将类实例化给一个对象赋予属性或者方法原型便于方法的重用与构造函数模式相比,使用原型对象的好处是可以让所有对象实例共享它所包含的属性和方法。

JavaScript面向对象主要知识点小结,基于ECMAScript 5.

构造函数
function People(name){
    //this可以理解为通过new即将创建的对象
    this.name = name;
}
//将类实例化
var person = new People("Cassie Xu");
console.log(person.name);

给一个对象赋予属性或者方法

function People(name) {
  this.name = name;
  this.greet = function() {
    console.log("Hello, my name is " + this.name + "!");
  };
}
var person = new People("Cassie Xu");
person.greet();
原型(prototype)

why we use prototype? -> 便于方法的重用
与构造函数模式相比,使用原型对象的好处是可以让所有对象实例共享它所包含的属性和方法。
运行时没找到函数的方法时,对象会首先找它的类的prototype方法
Demo1

function People(name) {
  this.name = name;
}
People.prototype = {
  greet: function() {
    console.log("Hello, my name is " + this.name + "!");
  }
};
var person = new People("Cassie Xu");
person.greet();
//在每个实例化后的对象都会有一个__proto__属性,类会将它的prototype赋给实例
//实例化一个对象时,People这个类首先会将person的__proto属性指向People.prototype
console.log(person.__proto__ === People.prototype); // true

Demo2

var a = { foo: 1 };
var b = { bar: 2 };
b.__proto__ = a;
//b本身没有foo属性,但是b会继续寻找__proto__属性
console.log(b.foo); // 1
console.log(b.bar); // 2

var a = {};
console.log(a.__proto__); // {}
console.log(a.__proto__.__proto__); // null
原型链(prototype chain)
var a = {};
console.log(a.__proto__); // {}
console.log(a.__proto__.__proto__); // null
继承(Inheritance)

Demo1

function Parent(){}
Parent.prototype = {
    greet: function(){
    console.log("hello world");
}
}
function Child(){}
//此方法适用于父类Child不需要传参数
Child.prototype = new Parent();
var c = new Child();
//c首先寻找自身的方法,没有great,所以找Child的原型方法,而Child.prototype等于Parent方法
c.greet(); //console.log("hello world");

上面的例子如果Parent有参数,将存在以下问题:

function Parent(a, b) {}
Parent.prototype.greet = function() {
  console.log("JavaScript rocks");
}
function Child(a, b) {}
Child.prototype = new Parent(); //something wrong?->new Parent()不能传参数,否则参数一直不变
var child = new Child();
child.greet();

Demo2
解决父类参数问题

function Parent() {
    this.name = "xxx",
    this.date = "xxx"
}
Parent.prototype.greet = function() {
  console.log("JavaScript rocks");
}
function Child() {}
//此方法适用于
Child.prototype = Object.create(Parent.prototype);
//硬记的知识 
Child.prototype.constructor = Child;
var child = new Child();
child.greet();

Demo3:继承的一个实例
创建一个矩形

function Rect(width,height){
    this._setupDOM(width,height);
}
Rect.prototype._setupDOM = function(width,height){
    this._dom = document.createElement("div");
    this._dom.style.width = width + "px";
    this._dom.style.height = height + "px";
};
Rect.prototype.appendToBody = function(){
    document.body.appendChild(this._dom);
};
function BlueRect(width,height){
  BlueRect._super.apply(this,arguments);
}
BlueRect.prototype = Object.create(Rect.prototype);
BlueRect.prototype.constructor = BlueRect;
BlueRect._super = Rect;
BlueRect.prototype._setupDOM = function(width,height){
  BlueRect._super.prototype._setupDOM.apply(this,arguments);
  this._dom.style.backgroundColor = "blue";
};
var br = new BlueRect(200,300);
br.appendToBody();
this关键字

Demo:

function Rect(width,height){
    //私有属性加_
    this._dom = document.createElement("div");
    this._dom.style.width = width + "px";
    this._dom.style.height = height + "px";
    this._dom.style.backgroundColor = "red";
}
Rect.prototype.appendToBody = function(){
    document.body.appendChild(this._dom);
};
var rect = new Rect(100,100);
rect.appendToBody();

修改上面demo:

function Rect(width,height){
    this._setupDom(width,height);
}
Rect.prototype._setupDom = function(width,height){
    //私有属性加_
    this._dom = document.createElement("div");
    this._dom.style.width = width + "px";
    this._dom.style.height = height + "px";
    this._dom.style.backgroundColor = "red";
};
Rect.prototype.appendToBody = function(){
    document.body.appendChild(this._dom);
};
var rect = new Rect(100,100);
rect.appendToBody();

var person = {
    firstName: "Penelope",
    lastName: "Barrymore",
    sayFullName: function () {
        console.log(this.firstName + " " + this.lastName); //=> "Penelope Barrymore"
        console.log(person.firstName + " " + person.lastName); //=> "Penelope Barrymore"
    }
};
person.sayFullName();

严格模式下的this

funtion foo(){
    "use strict";
    console.log(this) //undefined
}
强制修改函数上下文的方法 用Object.bind()代替this
function foo(a, b) {
  console.log(this);
  console.log(a + b);
}
var fooBinding = foo.bind({ name: "Cassie Xu" });
fooBinding(1, 2);

上面code将输出

[object Object] {
  name: "Cassie Xu"
}
使用Object.call/Object.apply执行上下文

call/apply方法都为调用Object方法,区别是apply将所有参数放到一个数组中去

function foo(a, b) {
  console.log(this);
  console.log(a + b);
}
foo.call({name:"Cassie Xu"}, 1, 2);
foo.apply({name:"Cassie Xu"}, [1, 2]);
补充其他 自执行函数

why we use it? ->避免泄露全局变量

(function(c){
    var a = 1;
    var b = 2;
    console.log(a+b+c);
})(3)
// c = 3
闭包作用域
var a = {};
a.foo = function(callback) {
  // do something and call callback in whatever way
}
a.bar = function() {
  this.num = 1;
  var that = this;
  //闭包,这里that可以访问到a.bar的作用域
  this.foo(function(newNum) {
    that.num = newNum;
  });
  console.log(this.num);
}
a.bar();

本文转自 JavaScript面向对象知识点小结

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85538.html

相关文章

  • 编程模型(范式)小结

    摘要:参考链接面向对象编程模型现在的很多编程语言基本都具有面向对象的思想,比如等等,而面向对象的主要思想对象,类,继承,封装,多态比较容易理解,这里就不多多描述了。 前言 在我们的日常日发和学习生活中会常常遇到一些名词,比如 命令式编程模型,声明式编程模型,xxx语言是面向对象的等等,这个编程模型到处可见,但是始终搞不清是什么?什么语言又是什么编程模型,当你新接触一门语言的时候,有些问题是需...

    miya 评论0 收藏0
  • javascript 面向对象版块之理解对象

    摘要:用代码可以这样描述安全到达国外面向过程既然说了面向对象,那么与之对应的就是面向过程。小结在这篇文章中,介绍了什么是面向对象和面向过程,以及中对象的含义。 这是 javascript 面向对象版块的第一篇文章,主要讲解对面向对象思想的一个理解。先说说什么是对象,其实这个还真的不好说。我们可以把自己当成一个对象,或者过年的时候相亲,找对象,那么你未来的老婆也是一个对象。我们就要一些属性,比...

    lovXin 评论0 收藏0
  • HTML5 Canvas游戏开发实战 PDF扫描版

    摘要:游戏开发实战主要讲解使用来开发和设计各类常见游戏的思路和技巧,在介绍相关特性的同时,还通过游戏开发实例深入剖析了其内在原理,让读者不仅知其然,而且知其所以然。HTML5 Canvas游戏开发实战主要讲解使用HTML5 Canvas来开发和设计各类常见游戏的思路和技巧,在介绍HTML5 Canvas相关特性的同时,还通过游戏开发实例深入剖析了其内在原理,让读者不仅知其然,而且知其所以然。在本书...

    cocopeak 评论0 收藏0
  • javascript基础篇小结

    摘要:表示尚未存在的对象是一个有特殊意义的值。可以为变量赋值为,此时变量的值为已知状态不是,即。用来初始化变量,清除变量内容,释放内存结果为但含义不同。且它俩与所有其他值比较的结果都是。,需要两个操作数同时转为。 转载请声明出处 博客原文 随手翻阅以前的学习笔记,顺便整理一下放在这里,方便自己复习,也希望你有也有帮助吧 第一课时 入门基础 知识点: 操作系统就是个应用程序 只要是应用...

    hiyang 评论0 收藏0

发表评论

0条评论

rubyshen

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<