前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于原型

关于原型

作者头像
爱学习的前端歌谣
发布2023-10-18 11:12:39
1110
发布2023-10-18 11:12:39
举报
文章被收录于专栏:前端小歌谣前端小歌谣

前言

我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是原型的讲解

环境配置

npm init -y

yarn add vite -D

修改page.json配置端口

代码语言:javascript
复制
{
  "name": "demo1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "vite --port 3002"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "vite": "^4.4.9"
  }
}

声明方式

代码语言:javascript
复制
const obj={}


function Obj1(){}


const obj1=new Obj1()


const obj2=Object.create(null)


const obj3=new Object()


console.log(obj,obj1,obj2,obj3)

运行结果

原型案例

代码语言:javascript
复制
function Test(){
    this.a=1;
    this.b=2;
}
Test.prototype.c=3
Test.prototype.d=4
Object.prototype.e=5
Object.prototype.f=6
const test=new Test()
console.log(test)

运行结果

原型案例

代码语言:javascript
复制
function test(){}
console.dir(test.__proto__===Function.prototype)
console.log(Function.__proto__===Function.prototype)
function Test1(){
    this.a=1
}
Test1.prototype.b=2
const test1=new Test1()
//test.__proto__
const testPrototype=Object.getPrototypeOf(test1)
console.log(testPrototype===test1.__proto__)
const obj={
    a:1
}
Object.setPrototypeOf(obj,{
    b:1
})
console.log(obj)

运行结果

对象参数属性

代码语言:javascript
复制
const obj=Object.create({
    a:1,
    b:2
},{
    a:{
        value:3,
        writable:false,
        configurable:true,
        enumerable:true
    }
})
console.log(obj)

监听状态改变

index.html

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>


    </h1>
    <h2></h2>
    <p></p>
    <span></span>
    <script type="module" src="./index5.js"></script>
</body>
</html>

index5.js

代码语言:javascript
复制
import { useReacttive, useWatcher } from "./Delfin.js"
const state = useReacttive({
    title: `This is title`,
    content: `This is content`
})
function render() {
    document.querySelector("h1").innerText = state.title
    document.querySelector("h2").innerText = state.title
    document.querySelector("p").innerText = state.content
    document.querySelector("span").innerText = state.content
}
render()
useWatcher([
    document.querySelector('h1'), document.querySelector('h2')
],'title',(prev,cur)=>{
    console.log(prev,cur)
})
useWatcher([
    document.querySelector('p'), document.querySelector('span')
],'content',(prev,cur)=>{
    console.log(prev,cur)
})
setTimeout(()=>{
    state.title="这是标题",
    state.content="这是内容"
},1000)

delfin.js

代码语言:javascript
复制
import WatcherMap from "./WacthMap"
import Watcher from './Watcher'


const watcherMap=new WatcherMap()
export function useReacttive(state){
   return new Proxy(state,{
    get(target,key){
        return Reflect.get(target,key)
    },
    set(target,key,value){
        watcherMap[key].update(target[key],value)
        return Reflect.set(target,key,value)
    }
   })
}


export function useWatcher(collection,dep,callback){
   const watcher=new Watcher()
   watcher.set(collection,callback)
   watcherMap.set(dep,watcher)
}

WatchMap.js

代码语言:javascript
复制
export default function WactherMap(){


}


WactherMap.prototype.set=function(dep,watcher){
    this[dep]=watcher;
}

Wacther.js

代码语言:javascript
复制
export default function Watcher(){
   this.collection=[];
   this.cb=null
}
Watcher.prototype.set=function(collection,callback){
   this.collection=collection
   this.cb=callback
}
Watcher.prototype.update=function(prevValue,currentValue){
  this.collection.forEach(el=>{
    el.innerText=currentValue
  })
  this.cb[prevValue,currentValue]
}

运行结果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端小歌谣 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档