关于原型

  前言

33071013375b68f7c5580c36bd43e35d.png

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

 环境配置

ced54d9bdfbf2a930468e0705719c511.png

npm init -y

yarn add vite -D

  修改page.json配置端口

7d00196e91fa0a76cb48f08a9a5c597e.png

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

声明方式

cc84eab1b242157fd32be90ae8448592.png

const obj={}
function Obj1(){}
const obj1=new Obj1()
const obj2=Object.create(null)
const obj3=new Object()
console.log(obj,obj1,obj2,obj3)

运行结果

ad287c8fe0a59ab0a757930fb9874896.png

4b959748d9926178296de06e5faaf790.png

原型案例

b50d971e17ae787a09721dd9865599e9.png

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)

运行结果

60a1eeb68cdec26a7a1ab62bcb2ec33e.png

6215ed66d25b48b770b9fb7559e58d6f.png

  原型案例

ecadebc121dc42eb2443046e8cddc190.png

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)

运行结果

e81f311813dc518fddacd776ad1d9290.png

02ecfdac6d4b455e9126a6d8eb76f591.png

对象参数属性

ac3dc15505c5ef1dd4d66315d6810913.png

const obj=Object.create({
a:1,
b:2
},{
a:{
value:3,
writable:false,
configurable:true,
enumerable:true
}
})
console.log(obj)

监听状态改变

700c4484a6f0b6be0c3d74eb59fdc2d2.png

  index.html

faf6c4ba2ef2a528508ea594a5c6975b.png

<!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

e15f411630f725ca759f5868f843abae.png

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

78dc0ad50c0791753fa29d9ed4ac7a01.png

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

dd1f76ecc6c5d57404082ecff7c226eb.png

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

 Wacther.js

a822d0c0a11edc242c7875079f235d10.png

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]
}

 运行结果

74f3d225f94c5ed7a526f0de88034643.png

0d4dcf282ebf857609294ec6fa438724.png

0ffba0fb0babb30a2225859b450a118d.png

b1ff18907d09f7078e42d8c7b0bb8842.png

点击上方 蓝字 关注我们

下方查看历史文章

21106e275456a8e2add4f7d2920eb601.png

关于this指向

关于回调

关于闭包(完)笔记

posted @   前端导师歌谣  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示