首页 > 编程语言 >JavaScript--原型及模块入门

JavaScript--原型及模块入门

时间:2022-10-28 19:55:33浏览次数:53  
标签:__ 入门 proto -- JavaScript Person 原型 prototype 属性

面向对象回顾

核心概念:万物皆对象 (顶层对象Object)抽取行为作为方法 抽取名词作为属性

俩种构建对象的方式

构造函数构建
es6的形式 class
class Person{
	constructor(){ //构造器
		this.name = 'jack'
	}
}
es3的形式 function
function Person(){
	this.name = 'jack'
}
使用new关键词
var person = new Person()
  • 自动构建对象
  • 手动设置属性
  • 自动返回对象
工厂构建
function factory(){
	var obj = new Object()
	obj.name = 'jack'
	return obj
}
调用方法
var person = factory()
  • 手动构建对象
  • 手动设置属性
  • 手动返回对象

prototype

概述:每个函数都会创建一个 prototype 属性,这个属性是一个对象,包含应该由特定引用类型的实例共享的属性和方法。他被称为显式原型

示例
function fn(){
	
}
//打印这个属性对应的空间
console.log(fn.prototype)
  • constructor 构造器他指回与之关联的构造函数。(指向对应的构建的函数指向他自己)
  • 根据上面的我们可以看到每个函数都有这样的一个属性,那么作为构造函数他是不是也有对应prototype属性
function Person(){
	
}
console.log(Person.prototype) //作为一个属性存在 属性是唯一 也就是当前他只有一个prototype 属性的声明只会声明一次
  • 作为一个属性存在 属性是唯一 也就是当前他只有一个prototype 属性的声明只会声明一次
  • 因为当前这个属性 他对应的是一个对象空间,所以他里面的内容可以被更改和设置的
  • 那么我们就可以在这个里面存放一些内容
function Person(){

}
Person.prototype.username = 'jack' //往这个对象空间存放一个属性 叫username
Person.prototype.age = 18 //往这个对象空间存放一个属性 叫age
Person.prototype.sayHello = ()=>{}
console.log(Person.prototype)
  • 通过实例对象 来访问对应的prototype里面的属性
//取出里面数据 因为构造函数是用于构建对象的 所以我们这个prototype的里面数据 是应该给实例对象去获取
var person = new Person()
//访问原型中数据(构造函数的prototype里面的属性)实例对象.属性名
console.log(person.username);
  • 因为上面讲过 对应的prototype是作为构造函数的属性 而构造函数的他只声明一次 那么对应的prototype也只声明一次
  • 也就意味者 你每次拿到的prototype是一个 也就是说他里面存放的数据也只声明一次
var person1 = new Person()
console.log(person1.sayHello == person.sayHello);//true
  • 由此可见 如果想对应的数据只声明一次的情况 我们可以放在原型里面

总结

  • 一般把方法放在原型里面
  • 把属性放在构造函数里面
function Son(){
    this.name = 'jack'
    this.age = 18
}
Son.prototype.print = function(){
    console.log(this); //指向当前的实例对象
    console.log(this.name); //指向当前的实例对象
}
new Son().print()

__proto__

概述:每个对象都有一个属性 叫做__proto__,他也是一个内存空间,他指向对应的构造函数的prototype。称为隐式原型。

示例
var obj = {}
console.log(obj.__proto__)

这个对象空间里面也可以存储对应的数据 这个__proto__ ,他是每个对象都有的,那么实例对象也是个对象他同样也有。

function Person(){

}
let person = new Person()
person.__proto__.username = 'jack'
console.log(person.__proto__)
//通过打印的结果 我们可以看到对应的里面的结果类似于我们的构造的prototype
console.log(Person.prototype == person.__proto__) //ture

从上述代码 大家可以看到对应的构造函数的prototype和对应的实例对象的__proto__ 是相等,那么也就证明了对应俩个内容其实是一个对象。那么我们就要明白 谁指向谁,从对应的先后顺序来看,我们知道先有的构造函数再有的实例对象,所以对应的实例对象的__proto__ 是指向对应的构造函数的prototype。那么这个内容是不是就是对象访问原型的属性(所以他又被称为隐式原型),所以我们在实际使用中并不会用到__proto__ ,而是通过这个属性来访问对应的显式原型

总结

  • 实例对象__proto__ 是指向对应构造函数的prototype
  • 在实际使用中一般不会用__proto__ ,是通过他来访问对应的prototype

从上可得对应的实例对象的__proto__ 是指向构造函数的prototype,那么请问对应的函数是不是也是一个对象,他同样具备__proto__ ,那么他的__proto__ 指向谁,最终又指向谁?

原型链

概述: 原型链其实就对应的寻找原型(__proto__)的过程组成的一个链式结构,称为原型链。

示例
Object.prototype.hello = 'hello'
class Person{
    constructor(){
        this.username = 'jack'
    }
}
Person.prototype.age = 18
class Son extends Person{
    constructor(){
        super()
    }
}
Son.prototype.say = ()=>{}
let son = new Son()
//son的__proto__指向构造函数的原型 对应的Son这个构造函数的__proto__又指向了对应的父类的原型
//对应的父类的构造的函数原型的__proto__指向了对应的Object的构造函数的原型 
//那么对应的Object的构造函原型的__proto__ 指向null 这个寻找的过程就称为原型链
console.log(son.__proto__.__proto__.__proto__.__proto__);
//那么接下来 我们又知道了对应的对象去访问原型的中属性 直接通过点的形式
//又明白了作用域链 是一层一层向上找变量作用域
//所以我们又可以通过原型链来找对应的属性 原型中的属性
console.log(son.age); //18
console.log(son.hello); //hello
console.log(son.abc); //undefined

这个寻找的过程就称为原型链

那么我想问请问对应的对象的属性赋值算不算原型链

不是 不是 不是!!!!

那么对象的属性赋值的操作有就覆盖 没有就添加

1.找的到就覆盖这个值

2.找不到就添加这个属性

总结

  • 如果在实例上添加了一个与原型对象中同名的属性,那就会在实例上创建这个属性,这个属性会遮住原型对象上的属性。

  • 只要给对象实例添加一个属性,这个属性就会遮蔽(shadow)原型对象上的同名属性,也就是虽然不会修改它,但会屏蔽对它的访问。即使在实例上把这个属性设置为 null,也不会恢复它和原型的联系。

  • 不过,使用 delete 操作符可以完全删除实例上的这个属性,从而让标识符解析过程能够继续搜索原型对象。

利用原型来实现对应的底层源码

实现对应的数组的高阶函数
//find findIndex
Array.prototype.myFind = function(fn){
    //先遍历数组
    for(var i=0;i<this.length;i++){
        if(fn(this[i],i,this)){
            return this[i]
        }
    }
}
Array.prototype.myFindIndex = function(fn){
    var index = -1
    //先遍历数组
    for(var i=0;i<this.length;i++){
        if(fn(this[i],i,this)){
            index = i
            break
        }
    }
    return index
}
//map forEach
Array.prototype.myMap = function(fn){
    var result = []
    //先遍历数组
    for(var i=0;i<this.length;i++){
        result.push(fn(this[i],i,this))
    }
    return result
}
Array.prototype.myForEach= function(fn){
    //先遍历数组
    for(var i=0;i<this.length;i++){
        fn(this[i],i,this)
    }
}

继承

概述:

继承属于面向对象的三大特性之一,面向对象的三大特性。封装 (抽取内容封装) 继承(子类继承父类) 多态(重写 重载)

继承为子类继承父类的内容,子类可以拥有父类所有的非私有的属性及方法。

继承的实现

extends 关键词(class的继承)*(es6的)
class Person{
	constructor(){
		this.username = 'jack'
	}
}
class Son extends Person{
	constructor(){
		 super()
	}
}
console.log(new Son().username) //jack
原型继承(prototype赋值给需要继承的对象)
function Person(){
	this.password = '123'
}
function Son(){

}
Son.prototype = new Person()
console.log(new Son().password )//123
组合继承 (原型+call改this指向)
//组合继承
function Person() {
    this.password = '123'
}
function Son() {
    Person.call(this) //将person里面的this改成Son里面的this
}
Son.prototype = new Person()
console.log(new Son().password) //123
对象冒充(改this指向 找不到原型上的内容(无法继承原型))
//对象冒充
function Person() {
    this.password = '123'
}

function Son() {
    Person.call(this) //将person里面的this改成Son里面的this
}
let son = new Son()
console.log(son.password);

多态

概述:一个东西多种形态体现,(水 冰 水汽)。子类是父类多态的体现(基于继承的)。

重写 子类重写父类的方法
function Person(){
	this.sayHello = ()=>{
		console.log('你好')
	}
}
function Son(){
	this.sayHello = ()=>{
		console.log('hello') //子类重写父类方法
	}
}
Son.prototype = new Person()
let son = new Son()
son.sayHello() //hello
重载 同一个类里面有多个同名的方法(覆盖 js没有重载)

重写应用

基础拖拽类
class Touch {
    constructor(ele) {
        this.ele = ele
        this.handlerDown()
    }
    handlerDown() {
        this.ele.onmousedown = (e) => {
            e = e || event
            //记录的是鼠标在div里面的位置
            //在mousedown里面记录按下的位置
            this.x = e.offsetX
            this.y = e.offsetY
            this.handlerMove()
            this.handlerUp()
        }
    }
    handlerMove() {
        document.onmousemove = (e) => {
            e = e || event
            //记录每次的位置  在document里面的位置
            var currentX = e.pageX
            var currentY = e.pageY
            //并且设置div的位置
            this.ele.style.left = currentX - this.x + "px"
            this.ele.style.top = currentY - this.y + "px"
        }
    }
    handlerUp() {
        //给document添加mouseup事件
        document.onmouseup = function () {
            //清除对应的mousemove事件
            document.onmousemove = null
        }
    }
}
var box = document.getElementById('box')
new Touch(box)
固定拖拽
class FixedTouch extends Touch {
    constructor(box, ele) {
        super(ele)
        this.box = box
    }
    handlerMove() {
        document.onmousemove = (e) => {
            e = e || event
            //记录每次的位置  在document里面的位置
            var currentX = e.pageX - getOffset(this.box).left
            var currentY = e.pageY - getOffset(this.box).top
            //范围判断
            this.target = {
                x: currentX - this.x,
                y: currentY - this.y
            }
            if (this.target.x < 0) {
                this.target.x = 0
            }
            if (this.target.y < 0) {
                this.target.y = 0
            }
            if (this.target.x > this.box.clientWidth - this.ele.offsetWidth) {
                this.target.x = this.box.clientWidth - this.ele.offsetWidth
            }
            if (this.target.y > this.box.clientHeight - this.ele.offsetHeight) {
                this.target.y = this.box.clientHeight - this.ele.offsetHeight
            }
            //并且设置div的位置
            this.ele.style.left = this.target.x + "px"
            this.ele.style.top = this.target.y + "px"
        }
    }
}
基于固定拖拽的放大镜
class Magnifier extends FixedTouch {
    constructor(smallBox, bigBox) {
        //调用父类构造
        super(smallBox, smallBox.querySelector('.move'))
        this.smallBox = smallBox
        this.bigBox = bigBox
        this.move = smallBox.querySelector('.move')
        this.bigImg = bigBox.children[0]
        this.init()
        this.handlerHover()
        //取消按下的事件
        this.handlerDown = () => {}
        //调用move 
        this.handlerMove()
    }
    init() {
        //计算对应的move这个盒子的宽高
        // 大的比大的等于小的比小的 bigImg/bigBox = box/move ==>  bigImg/box == bigBox/move
        this.move.style.width = this.smallBox.clientWidth / (this.bigImg.clientWidth / this.bigBox
            .clientWidth) + 'px'
        this.move.style.height = this.smallBox.clientHeight / (this.bigImg.clientHeight / this.bigBox
            .clientHeight) + 'px'
        //先需要隐藏
        this.move.style.display = 'none'
        this.bigBox.style.display = 'none'
    }
    handlerHover() {
        //移入移出
        this.smallBox.onmouseenter = () => {
            this.move.style.display = 'block'
            this.bigBox.style.display = 'block'
            //x y的属性值
            this.x = this.move.clientWidth / 2,
                this.y = this.move.clientHeight / 2
        }
        this.smallBox.onmouseleave = () => {
            this.move.style.display = 'none'
            this.bigBox.style.display = 'none'
        }
        //添加一个移动的事件
        this.smallBox.onmousemove = () => {
            //改变大盒子里面图片的位置
            this.bigImg.style.left = -this.target.x * this.bigImg.clientWidth / this.smallBox.clientWidth + 'px'
            this.bigImg.style.top = -this.target.y * this.bigImg.clientHeight / this.smallBox.clientHeight + 'px'
        }
    }
}

es6的模块化

ECMAScript 6 模块是作为一整块 JavaScript 代码而存在的。带有 type="module"属性的

标签:__,入门,proto,--,JavaScript,Person,原型,prototype,属性
From: https://www.cnblogs.com/zhouwying/p/16837238.html

相关文章

  • JavaScript--条件控制语句
    一、if语句if语句有三种形式:简单的if语句,if-else语句,if-elseif语句。1、语法if语句是使用最频繁的语句之一,语法如下://if语句if(condition){statement1}......
  • JavaScript--详解&&和--
    一、&&和||的简单介绍1、&&(逻辑与)逻辑与操作符由两个和号(&&)表示。2、||(逻辑或)逻辑或操作符由两个管道符(||)表示。二、深入理解&&和||1、用法&&和||运算符的返回值......
  • TreeMap
    (1)TreeMap跟TreeSet底层原理一样,都是红黑树结构的。(2)由键决定特性:不重复、无索引、可排序。(3)可排序:对键进行排序。(4)注意:默认按照键的从小到大进行排序,也可以自己规定键的......
  • 软链接和硬链接
    软链接:1.软链接存放的是源文件的路径,指向源文件2.源文件删除,软链接文件依然存在,但是找不到路径3.源文件和软链接文件iNode号不同4.创建软链接  ln -s  源文件......
  • 程序员的修炼之道——从小工到专家4
    第四章注重实效的偏执 按合约设计没有合约,编译器所能做的只是确保子类符合特定的方法型构(signature)。但如果我们适当设定基类合约,我们现在就能够确保将来任何子类都无......
  • PYTHON JSON EXCEL
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#pipinstallpandas#pipinstallopenpyxl#importjsonimporttimeimportpandasimpor......
  • python 爬虫 -----Bs4 爬取并且下载图片
    #1.拿到主页面主代码,拿到子页面连接地址,href#2.通过href拿到子页面内容,从子页面中找到图片的下载地址img->src#3.下载图片importrequestsfrombs4importBea......
  • 实验6_开源控制器实践——Ryu
    一)基本要求1.搭建所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。·构建拓扑sudomn--topo=single,3--mac--controller=remote,ip......
  • 互联网基础
    二、传输层:TCP协议1.保证所有数据都能被接收端接收,会重发受损数据。UDP协议1.比TCP速度快,不能保证接收所有的数据,有损坏的数据会被抛弃,不会重发受损数据。三、应用......
  • chap4 循环结构、chap6 回顾数据类型和表达式 第七小组 学习总结
    chap4循环结构第七小组学习总结循环语句有以下几种:1.for循环语句 (1)使用方法:     for(初始条件;循环条件;结束操作){循环语句;}(2)优点:循环有序,层层递进  ......