<a>
标签,点击的时候弹出来对应的序号JS变量类型、强制类型转换、浏览器渲染过程、做用域、JS模块化、JS基础算法javascript
JS “三座大山” —— 原型、做用域和异步php
var a=100
var b=a
a=200
console.log(b) // 100
复制代码
引用类型:对象、数组、函数css
var a={age:20}
var b=a
b.age=21
console.log(a.age)
复制代码
typeof undefined // undefined
typeof 'abc' // string
typeof 123 // number
typeof true // boolean
typeof {} // object
typeof [] // object
typeof null // object
typeof console.log // function
复制代码
var a=100+10 // 110
var b=100+'10' // '10010'
复制代码
100=='100' // true
0=='' // true
null==undefined // true
复制代码
var a=true
if (a) {
// ...
}
var b=100
if (b) {
//...
}
var c=''
if (c) {
//...
}
复制代码
console.log(10&&0); // 0
console.log(''||'abc'); // 'abc'
console.log(!window.abc); //true
// 判断一个变量会被当作true仍是false
var a=100
console.log(!!a);
复制代码
JS中使用typeof能获得的哪些类型html
什么时候使用===什么时候使用==前端
// 问题:什么时候使用=== 什么时候使用==
if (obj.a==null) {
// 这里至关于obj.a===null||obj.a===undefined,简写形式
// 这是jquery源码中推荐的写法
}
复制代码
JS中有哪些内置函数java
// 问题:JS中有哪些内置函数--数据封装类对象
Object
Array
Boolean
Number
String
Function
Date
RegExp
Error
复制代码
JS变量按照存储方式区分为哪些类型,并描述其特色node
如何理解JSONjquery
// 问题:如何理解JSON
// JSON只不过是一个JS对象而已
JSON.stringify({a:10,b:20})
JSON.parse('{"a":10,"b":20}')
复制代码
function Foo(name,age){
this.name=name
this.age=age
this.class='class-1'
// return this 默认有这一行
}
var f=new Foo('zhangsan',20)
// var f1=new Foo('lisi',22) 建立多个对象
复制代码
var a={}
实际上是var a=new Object()
的语法糖var a=[]
实际上是var a=new Array()
的语法糖function Foo(){...}
实际上是var Foo=new Function(...)
instanceof
判断一个函数是不是一个变量的构造函数🐖:判断一个变量是否为“数组”:变量 instanceof Array
linux
5条原型规则git
原型规则是学习原型链的基础
全部的引用类型(数组、对象、函数),都具备对象特性,便可自由扩展属性(除了“null”之外)
var obj={}; obj.a=100;
var arr=[]; arr.a=100;
function fn(){}
fn.a=100;
复制代码
全部的引用类型(数组、对象、函数),都有一个__proto__
属性,属性值是一个普通的对象(🐖:即隐式原型)
console.log(obj.__proto__);
console.log(arr.__proto__);
console.log(fn.__proto__);
复制代码
全部的函数,都有一个prototype
属性,属性值也是一个普通的对象(🐖:即显示原型)
console.log(fn.prototype);
复制代码
全部的引用类型(数组、对象、函数),__proto__
属性值指向它的构造函数的“prototype
”属性值
console.log(obj.__proto__===Object.prototype)
复制代码
当试图获得一个对象的某个属性时,若是这个对象自己没有这个属性,那么会去它的__proto__
(即它的构造函数的prototype
)中寻找。
// 原型规则和示例
// 构造函数
function Foo(name,age){
this.name=name
}
Foo.prototype.alertName=function(){
alert(this.name)
}
// 建立示例
var f=new Foo('zhangsan')
f.printName=function(){
console.log(this.name)
}
// 测试
f.printName()
f.alertName()
复制代码
var item
for(item in f){
// 高级浏览器已经在for in 中屏蔽了来自原型的属性
// 可是这里建议你们仍是加上这个判断,保证程序的健壮性
if(f.hasOwnProperty(item)){
console.log(item)
}
}
复制代码
f.toString() // 要去f.__proto__.__proto__中查找
复制代码
🐖:用于判断引用类型属于哪一个构造函数的方法
f instanceof Foo
的判断逻辑是:__proto__
一层一层往上,可否对应到Foo.prototypef instanceof Object
如何准确判断一个变量是数组类型
var arr=[]
arr instanceof Array // true
typeof arr // object,typeof是没法判断是不是数组的
复制代码
写一个原型链继承的例子
// 动物
function Animal(){
this.eat=function(){
console.log('animal eat')
}
}
// 狗
function Dog(){
this.bark=function(){
console.log('dog bark')
}
}
Dog.prototype=new Animal()
// 哈士奇
var hashiqi=new Dog()
// 接下来的代码演示时,会推荐更加贴近实战的原型链继承示例!
复制代码
描述new一个对象的过程
zepto(或其余框架)源码中如何使用原型链
// 封装DOM查询
function Elem(id) {
this.elem=document.getElementById(id)
}
Elem.prototype.html=function (val) {
var elem=this.elem
if (val) {
elem.innerHTML=val
return this; // 为了链式操做
}else{
return elem.innerHTML
}
}
Elem.prototype.on=function (type,fn) {
var elem=this.elem
elem.addEventListener(type,fn)
return this
}
Elem.prototype.getStyle=function(elem,attr){
if (elem.currentStyle) {
return elem.currentStyle[attr]
}else {
return getComputedStyle(elem,false)[attr]
}
}
Elem.prototype.css=function (attr,val) {
var elem=this.elem
if (val) {
elem.style[attr]=val
return this
}else {
return this.getStyle(elem,attr)
}
}
var div1=new Elem('box')
div1.html('123').on('click',function () {
alert('click')
}).css('border','1px solid #f00');
复制代码
<a>
标签,点击的时候弹出来对应的序号<script>
或者一个函数<script>
PS: 注意“函数声明”和“函数表达式”的区别
var a={
name:'A',
fn:function(){
console.log(this.name);
}
}
a.fn() // this===a
a.fn.call({name:'B'}) // this==={name:'B'}
var fn1=a.fn
fn1() // this===window
复制代码
// 无块级做用域
if (true) {
var name='zhangsan'
}
console.log(name);
// 函数和全局做用域
var a=100
function fn(){
var a=200
console.log('fn',a)
}
console.log('global',a);
fn()
复制代码
var a=100
function fn(){
var b=200
// 当前做用域没有定义的变量,即“自由变量”
console.log(a);
console.log(b);
}
fn()
var a=100
function F1(){
var b=200
function F2(){
var c=200
console.log(a); // a是自由变量
console.log(b); // b是自由变量
console.log(c);
}
F2()
}
F1()
复制代码
function F1(){
var a=100
// 返回一个函数(函数做为返回值)
return function(){
console.log(a)
}
}
// f1获得一个函数
var f1=F1()
var a=200
f1()
复制代码
说一下对变量提高的理解
说明this几种不一样的使用场景
建立10个<a>
标签,点击的时候弹出来对应的序号 🐖:自执行函数,就是不用调用,只要定义完成,当即执行的函数
// 这是一个错误的写法!!!
var i,a
for(i=0;i<10;i++){
a=document.createElement('a')
a.innerHTML=i+'<br>'
a.addEventListener('click',function(e){
e.preventDefault()
alert(i)
})
document.body.appendChild(a)
}
// 这是正确的写法!!!
var i
for(i=0;i<10;i++){
(function(i){
var a=document.createElement('a')
a.innerHTML=i+'<br>'
a.addEventListener('click',function(e){
e.preventDefault()
alert(i)
})
document.body.appendChild(a)
})(i)
}
复制代码
如何理解做用域
实际开发中闭包的应用
// 闭包实际开发中主要用于封装变量,收敛权限
function isFirstLoad(){
var _list=[]
return function (id) {
if (_list.indexOf(id)>=0) {
return false
}else {
_list.push(id)
return true
}
}
}
// 使用
var firstLoad=isFirstLoad()
firstLoad(10) // true
firstLoad(10) // false
firstLoad(20) // true
复制代码
console.log(100);
setTimeout(function(){
console.log(200);
},1000)
console.log(300);
复制代码
console.log(100);
alert(200) // 1秒钟以后点击确认
console.log(300);
复制代码
<img>
加载<img>
加载示例console.log('start');
var img=document.createElement('img')
img.onload=function(){
console.log('loaded');
}
img.src='/xxx.png'
console.log('end');
复制代码
console.log('start');
document.getElementById('btn1').addEventListener('click',function(){
alert('clicked')
})
console.log('end');
复制代码
// 示例1 定时器
console.log(100);
setTimeout(function(){
console.log(200);
},1000)
console.log(300);
复制代码
// 示例2 ajax请求
console.log('start');
$.get('./data1.json',function(data1){
console.log(data1);
})
console.log('end');
复制代码
同步和异步的区别是什么?分别举一个同步和异步的例子
一个关于setTimeout的笔试题
console.log(1);
setTimeout(function(){
console.log(2);
},0)
console.log(3);
setTimeout(function(){
console.log(4);
},1000)
console.log(5);
复制代码
前端使用异步的场景有哪些
Date.now() // 获取当前时间毫秒数
var dt=new Date()
dt.getTime() // 获取毫秒数
dt.getFullYear() // 年
dt.getMonth() // 月(0-11)
dt.getDate() // 日(0-31)
dt.getHours() // 小时(0-23)
dt.getMinutes() // 分钟(0-59)
dt.getSeconds() // 秒(0-59)
复制代码
Math.random()
var arr=[1,2,3]
arr.forEach(function(item,index){
// 遍历数组的全部元素
console.log(index,item);
})
复制代码
var arr=[1,2,3]
var result=arr.every(function(item,index){
// 用来判断全部的数组元素,都知足一个条件
if(item<4){
return true
}
})
console.log(result);
复制代码
var arr=[1,2,3]
var result=arr.some(function(item,index){
// 用来判断全部的数组元素,只要有一个知足条件便可
if(item<2){
return true
}
})
console.log(result);
复制代码
var arr=[1,4,2,3,5]
var arr2=arr.sort(function(a,b){
// 从小到大排序
return a-b
// 从大到小排序
// return b-a
})
console.log(arr2);
复制代码
var arr=[1,2,3,4]
var arr2=arr.map(function(item,index){
// 将元素从新组装,并返回
return '<b>'+item+'</b>'
})
console.log(arr2);
复制代码
var arr=[1,2,3]
var arr2=arr.filter(function(item,index){
// 经过某一个条件过滤数组
if(item>=2){
return true
}
})
console.log(arr2);
复制代码
var obj={
x:100,
y:200,
z:300
}
var key
for (key in obj) {
// 注意这里的hasOwnProperty,再讲原型链的时候讲过了
if (obj.hasOwnProperty(key)) {
console.log(key,obj[key]);
}
}
复制代码
获取2017-06-10格式的日期
function formatDate(dt){
if (!dt) {
dt=new Date()
}
var year=dt.getFullYear()
var month=dt.getMonth()+1
var date=dt.getDate()
if (month<10) {
// 强制类型转换
month='0'+month
}
if (date<10) {
// 强制类型转换
date='0'+date
}
// 强制类型转换
return year+'-'+month+'-'+date
}
var dt=new Date()
var formatDate=formatDate(dt)
console.log(formatDate);
复制代码
获取随机数,要求是长度一致的字符串格式
var random=Math.random()
var random=random+'0000000000' // 后面加上10个0
var random=random.slice(0,10)
console.log(random);
复制代码
写一个能遍历对象和数组的通用forEach函数
function forEach(){
var key
if(obj instanceof Array){
// 准确判断是否是数组
obj.forEach(function(item,index){
fn(index,item)
})
}else{
// 不是数组就是对象
for(key in obj){
fn(key,obj[key])
}
}
}
var arr=[1,2,3]
// 注意,这里参数的顺序换了,为了和对象的遍历格式一致
forEach(arr,function(index,item){
console.log(index,item);
})
var obj={x:100,y:200}
forEach(obj,function(key,value){
console.log(key,value);
})
复制代码
Document Object Model
<!-- xml -->
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</form>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<other>
<a></a>
<b></b>
</other>
</note>
<!-- html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<p>this is p</p>
</div>
</body>
</html>
复制代码
DOM能够理解为:浏览器把拿到的html代码,结构化一个浏览器能识别而且js可操做的一个模型而已。
var div1=document.getElementById('div1') // 元素
var divList=document.getElementsByTagName('div') // 集合
console.log(divList.length);
console.log(divList[0]);
var containerList=document.getElementsByClassName('.container') // 集合
var pList=document.querySelectorAll('p') // 集合
复制代码
var pList=document.querySelectorAll('p') // 集合
var p=pList[0]
console.log(p.style.width); // 获取样式
p.style.width='100px' // 修改样式
console.log(p.className); // 获取class
p.className='p1' // 修改class
// 获取nodeName和nodeType
console.log(p.nodeName);
console.log(p.nodeType);
复制代码
var pList=document.querySelectorAll('p') // 集合
var p=pList[0]
p.getAttribute('data-name')
p.getAttribute('data-name','imooc')
p.getAttribute('style')
p.getAttribute('style','font-size:30px;')
复制代码
var div1=document.getElementById('div1')
// 添加新节点
var p1=document.createElement('p')
p1.innerHTML='this is p1'
div1.appendChild(p1) // 添加新建立的元素
// 移动已有节点
var p2=document.getElementById('p2')
div1.appendChild(p2)
复制代码
var div1=document.getElementById('div1')
var parent=div1.parentElement
var child=div1.childNodes
div1.removeChild(child[0])
复制代码
var div1=document.getElementById('div1')
var child=div1.childNodes
div1.removeChild(child[0])
复制代码
Browser Object Model
// navigator
var ua=navigator.userAgent
var isChrome=ua.indexOf('Chrome')
console.log(isChrome);
// screen
console.log(screen.width);
console.log(screen.height);
复制代码
// location
console.log(location.href);
console.log(location.protocol); // 'http:','https:'
console.log(location.pathname); // '/learn/199'
console.log(location.search);
console.log(location.hash);
// history
history.back()
history.forward()
复制代码
如何检测浏览器的类型
拆解url的各部分
var btn=document.getElementById('btn1')
btn.addEventListener('click',function(event){
console.log('clicked');
})
function bindEvent(elem,type,fn){
elem.addEventListener(type,fn)
}
var a=document.getElementById('link1')
bindEvent(a,'click',function(e){
e.preventDefault() // 阻止默认行为
alert('clicked')
})
复制代码
<div id="div1">
<p id="p1">激活</p>
<p id="p2">取消</p>
<p id="p3">取消</p>
<p id="p4">取消</p>
</div>
<div id="div2">
<p id="5">取消</p>
<p id="6">取消</p>
</div>
<script> var p1 = document.getElementById('p1') var body = document.body bindEvent(p1, 'click', function (e) { e.stopPropatation() alert('激活') }) bindEvent(body, 'click', function (e) { alert('取消') }) </script>
复制代码
<div id="div1">
<a href="#">a1</a>
<a href="#">a2</a>
<a href="#">a3</a>
<a href="#">a4</a>
<!-- 会随时新增更多a标签 -->
</div>
<script> var div1=document.getElementById('div1') div1.addEventListener('click',function(e){ var target=e.target if(target.nodeName==='A'){ alert(target.innetHTML) } }) </script>
复制代码
// 使用代理
var div1=document.getElementById('div1')
bindEvent(div1,'click','a',function(e){
console.log(this.innerHTML);
})
// 不使用代理
var a=document.getElementById('a1')
bindEvent(div1,'click',function(e){
console.log(a.innerHTML);
})
复制代码
var xhr=new XMLHttpRequest()
xhr.open('get','/api',false)
xhr.onreadystatechange=function () {
// 这里的函数异步执行,可参考以前JS基础中的异步模块
if (xhr.readyState === 4) {
if (xhr.statusCode === 200) {
alert(xhr.responseText)
}
}
}
xhr.send(null)
复制代码
xhr.readyState==4
什么是跨域
能够跨域的三个标签
<img src=xxx>
<link href=xxxx>
<script src=xxx>
三个标签的场景
<img>
用于打点统计,统计网站多是其余域<link><script>
可使用CDN,CDN的也是其余域<script>
能够用于JSONP跨域的注意事项
JSONP实现原理
http://coding.m.imooc.com/classindex.html
<script src="http://coding.m.imooc.com/api.js">
http://coding.m.imooc.com/api.js
<script> window.callback=function(data){ // 这是咱们跨域获得的信息 console.log(data); } </script>
<script src="http://coding.m.imooc.com/api.js"></script>
<!-- 以上将返回callback({x:100,y:200}) -->
复制代码
服务器端设置http header
document.cookie=...
获取和修改便可// util.js
function getFormatDate(date,type){
// type===1 返回2017-06-15
// type===2 返回2017年6月15日
// ...
}
// a-util.js
function aGetFormatDate(date){
// 要求返回2017年6月15日 格式
return getFormatDate(date,2)
}
// a.js
var dt=new Date()
console.log(aGetFormatDate(dt));
复制代码
<script src="/static/js/jquery.js"></script>
<script>
时,会执行并阻塞渲染<script src="abc_1.js"></script>
<script src="abc_2.js"></script>
<script>
<
为<
<img src=xxx.com/pay?id=100>