当前位置:网站首页>对象的访问机制及其他
对象的访问机制及其他
2022-06-27 00:33:00 【InfoQ】
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHi = function () { console.log('hello world') }
// 使用 Person 创建一个对象
let p1 = new Person('Jack', 18);
console.log(p1);
// 当我访问 p1.name 的时候, 自己就有
console.log(p1.name);
p1.sayHi();
// 再次创建一个实例化对象
let p2 = new Person('Rose', 20);
p2.sayHi();
function Tabs(ele, options = {}) {
// 拿到出现选项卡的范围
this.ele = document.querySelector(ele)
// 找到 btns
this.btns = this.ele.querySelectorAll('ul > li')
// 找到 tabs
this.tabs = this.ele.querySelectorAll('ol > li')
// 初始化一下 options
this.options = options
this.change()
}
Tabs.prototype.change = function () {
// 操作的是当前实例的 btns 和 tabs
// this 就是当前实例, 我们就要给 this.btns 的每一个添加点击事件
this.btns.forEach((item, index) => {
item.addEventListener(this.options.type || 'click', () => {
this.btns.forEach((t, i) => {
t.className = ''
this.tabs[i].className = ''
})
// 给对应的添加类名
item.className = 'active'
this.tabs[index].className = 'active'
})
})
}
new Tabs('.box2', { type: 'mouseover' })
new Tabs('.box3');
let t1 = new Tabs('.box', { type: 'click' })
console.log(t1);
1. var arr = [] , Array 的实例
2. var obj = {} , Object 的实例
3. var p1 = new Person() , Person 的实例
4. var time = new Date() , Date 的实例
5. var fn = function () {} , Function 的实例
6. Person.prototype , Object 的实例
7. Array.prototype , Objec 的实例
原型链
判断数据类型
- instanceof
了解对象
for in 循环
对象自己的方法
边栏推荐
- Custom MVC (imported into jar package) + difference from three-tier architecture + reflection + interview questions
- idea 热启动失效解决方案
- 用代码生成流程图,Markdown的使用方法
- 接口测试框架实战(一) | Requests 与接口请求构造
- LeetCode 142. 环形链表 II
- Interface test framework practice (I) | requests and interface request construction
- 数据库面试题+sql语句解析
- Central Limit Theorem
- CH423要如何使用,便宜的国产IO扩展芯片
- Central Limit Theorem
猜你喜欢
随机推荐
Concepts de base de données Oracle
这10款文案神器帮你速码,做自媒体还担心写不出文案吗?
Timing mechanism of LwIP
小白看MySQL--windows环境安装MySQL
Solve the problem that only one line of text is displayed or not displayed in u8glib
滑环安装有哪些技巧和方法
memcached基础6
Hid device descriptor and keyboard key value corresponding coding table in USB protocol
memcached基础1
find_circ详细使用指南
Pet hospital management system based on SSMP
Reading graph augmentations to learn graph representations (lg2ar)
Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022
05 | 規範設計(下):commit 信息風格迥异、難以閱讀,如何規範?
How to measure the thickness of glass substrate by spectral confocal
The world is very big. Some people tattoo QR codes on their necks
Play OLED, u8g2 animation, increasing numbers, random triangles, etc
Simple and fast digital network (network dolls in the network)
Account management, database building and four engines + cases of MySQL
Oracle database basics concepts