
- function Star(uname,age) {
- this.uname = uname
- this.age = age
- }
- console.log(Star.prototype)//返回一个对象称为原型对象
- Star.prototype.sing = function () {
- console.log('我会唱歌')
- }
- const ldh = new Star('刘德华',19)
- const zxy = new Star('张学友',20)
- console.log(ldh.sing === zxy.sing) //结果是true
示例:
- Array.prototype.max = function () {
- return Math.max(...this)
- }
- console.log([1,2,3].sum())
- Array.prototype.sum = function(){
- return this.reduce((prev,item) => prev + item, 0)
- }
- console.log([1,2,3].sum())
每个原型对象里面都有个constructor 属性(constructor 构造函数),该属性指向该原型对象的构造函数

使用场景:
如果有多个对象的方法,我们可以给原型对象采取对象形式赋值.
但是这样就会覆盖构造函数原型对象原来的内容,这样修改后的原型对象 constructor 就不再指向当前构造函数了
此时,我们可以在修改后的原型对象中,添加一个 constructor 指向原来的构造函数。
- function Star(name) {
- this.name = name
- }
- Star.prototype = {
- sing:function(){ console.log('唱歌') },
- dance:function(){ console.log('跳舞') }
- }
- console.log(Star.prototype.constructor) // 指向Object
- function Star(name) {
- this.name = name
- }
- Star.prototype = {
- constructor:Star
- sing:function(){ console.log('唱歌') },
- dance:function(){ console.log('跳舞') }
- }
- console.log(Star.prototype.constructor) //指向Star

- const People = {
- head:1,
- eyes:2
- }
-
- function Man() {}
- function Woman() {}
- Man.prototype = People
- Man.prototype.constructor = Man
- const pink = new Man()
- console.log(pink)
- Woman.prototype = People // {eyes: 2, head: 1}
- Woman.prototype.constructor = Woman
-
- // 给女人添加一个方法 生孩子
- Woman.prototype.baby = function () {
- console.log('宝贝')
- }
- const red = new Woman()
- console.log(red)

4.解决:
- function Person() {
- this.eyes = 2
- this.head = 1
- }
- function Woman() {}
- Woman.prototype = new Person() // {eyes: 2, head: 1}
- Woman.prototype.constructor = Woman
-
- // 给女人添加一个方法 生孩子
- Woman.prototype.baby = function () {
- console.log('宝贝')
- }
- const red = new Woman()
- console.log(red)
- // console.log(Woman.prototype)
- function Man() {}
- // 通过 原型继承 Person
- Man.prototype = new Person()
- Man.prototype.constructor = Man
- const pink = new Man()
- console.log(pink)
- DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>面向对象封装消息提示title>
- <style>
- .modal {
- width: 300px;
- min-height: 100px;
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
- border-radius: 4px;
- position: fixed;
- z-index: 999;
- left: 50%;
- top: 50%;
- transform: translate3d(-50%, -50%, 0);
- background-color: #fff;
- }
-
- .modal .header {
- line-height: 40px;
- padding: 0 10px;
- position: relative;
- font-size: 20px;
- }
-
- .modal .header i {
- font-style: normal;
- color: #999;
- position: absolute;
- right: 15px;
- top: -2px;
- cursor: pointer;
- }
-
- .modal .body {
- text-align: center;
- padding: 10px;
- }
-
- .modal .footer {
- display: flex;
- justify-content: flex-end;
- padding: 10px;
- }
-
- .modal .footer a {
- padding: 3px 8px;
- background: #ccc;
- text-decoration: none;
- color: #fff;
- border-radius: 2px;
- margin-right: 10px;
- font-size: 14px;
- }
-
- .modal .footer a.submit {
- background-color: #369;
- }
- style>
- head>
-
- <body>
- <button id="delete">删除button>
- <button id="login">登录button>
-
-
-
-
- <script>
- // 1. 模态框的构造函数
- function Modal(title = '', message = '') {
- // 公共的属性部分
- this.title = title
- this.message = message
- // 因为盒子是公共的
- // 1. 创建 一定不要忘了加 this
- this.modalBox = document.createElement('div')
- // 2. 添加类名
- this.modalBox.className = 'modal'
- // 3. 填充内容 更换数据
- this.modalBox.innerHTML = `
- ${this.title} x
- ${this.message}
- `
- // console.log(this.modalBox)
- }
- // 2. 打开方法 挂载 到 模态框的构造函数原型身上
- Modal.prototype.open = function () {
- if (!document.querySelector('.modal')) {
- // 把刚才创建的盒子 modalBox 渲染到 页面中 父元素.appendChild(子元素)
- document.body.appendChild(this.modalBox)
- // 获取 x 调用关闭方法
- this.modalBox.querySelector('i').addEventListener('click', () => {
- // 箭头函数没有this 上一级作用域的this
- // 这个this 指向 m
- this.close()
- })
- }
- }
- // 3. 关闭方法 挂载 到 模态框的构造函数原型身上
- Modal.prototype.close = function () {
- document.body.removeChild(this.modalBox)
- }
-
- // 4. 按钮点击
- document.querySelector('#delete').addEventListener('click', () => {
- const m = new Modal('温馨提示', '您没有权限删除')
- // 调用 打开方法
- m.open()
- })
-
- // 5. 按钮点击
- document.querySelector('#login').addEventListener('click', () => {
- const m = new Modal('友情提示', '您还么有注册账号')
- // 调用 打开方法
- m.open()
- })
-
- script>
- body>
-
- html>