前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >zepto设计和源码分析(推荐) 原

zepto设计和源码分析(推荐) 原

作者头像
晓歌
发布2018-08-15 14:57:48
3670
发布2018-08-15 14:57:48
举报
文章被收录于专栏:破晓之歌破晓之歌

课程地址:https://www.imooc.com/learn/745

一、简单介绍

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" http-equiv="X-UA-Compatible" content="IE=edge,width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0">
    <title>zepto</title>
    <style>
        .blue{color:blue;}
    </style>
    <script src="https://cdn.bootcss.com/zepto/1.1.6/zepto.js"></script>
</head>
<body>
    <p id="p1">测试</p>
    <div>
        <span>test</span>
        <span>test</span>
        <span>test</span>
    </div>
    <script >
        var $p1 = $('#p1')
        var $span = $('span')
        console.log($p1);
        console.log($span);
    </script>
</body>
</html>
代码语言:javascript
复制
arr.__proto__.addClass = function(){alert(456)}

二、js原型基础

constructor是指向这个对象本身

这两种写法完全一样

写法等价

数组:

整理:

代码语言:javascript
复制
//第一句话----------------每个函数都有一个prototype属性
var fn = function(){}
fn.prototype
fn === fn.prototype.constructor

//第二句话----------------所有通过函数new出来的东西,这个东西都有一个__proto__指向这个函数的prototype
//空函数
var fn = new Function()
fn.__proto__
fn === arr.prototype.constructor//true

//数组
var arr = []//等同下面一行的写法
var arr = new Array()//写法2,等同上一行
arr.__proto__
arr === arr.prototype.constructor//true

//对象
var a = {}//等同下面一行的写法
var a = new Object()//写法2,等同上一行
a.__proto__
a === arr.prototype.constructor//true


//第三句话----------------当你想要使用一个对象(或者一个数组)的某个功能时:如果该对象本身具有这个功能,则直接使用;如果该对象本身没有这个功能,则去__proto__中找

//举例
arr.push(1)
arr//[1]
arr.__proto__.addClass= function(){alert(111)}//数组没有addClass方法,在__proto__自定义添加
arr1.addClass()//执行,结果是弹出111的对话框



//----------------原理:原型对象
Array
Array.prototype
Object
Object.prototype
Function
Function.prototype

//等价写法
[].concat === Array.prototype.concat;//true
arr.__proto__ === Array.prototype;//true

三、源码分析

3.1分析结构

代码语言:javascript
复制
var arr = [1,2,3]
arr.__proto__ ={
    addCLass:function(){
        console.log('this is addClass')
    },
    concat:Array.prototype.concat,
    push:Array.prototype.push
}

//验证
var $p = $('p');
// 使用constructor验证
arr.__proto__.constructor === $p.__proto__.constructor//true

// 使用instanceof验证
console.log($p instanceof Array);//false
console.log(arr instanceof Array);//false

插件机制

双斜线支持http,https,需要cdn支持

之上是core模块的内容

3.2分析inint函数

视频附加信息链接:http://www.kancloud.cn/wangfupeng/zepto-design-srouce/173680

qsa

返回数组

返回函数

3.3 Z函数

空数组

IE低版本不支持

3.4对比新旧zpeto的版本

四、总结

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简单介绍
  • 二、js原型基础
  • 三、源码分析
    • 3.1分析结构
      • 3.2分析inint函数
        • 3.3 Z函数
          • 3.4对比新旧zpeto的版本
          • 四、总结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档