《JavaScript面向对象编程指南(第2版)》读书笔记(二)
- 《JavaScript面向对象编程指南(第2版)》读书笔记(一)
- 《JavaScript面向对象编程指南(第2版)》读书笔记(二)
目录
一、基本类型
1.1 字符串
1.2 对象
1.3 原型
1.4 常用数学方法
二、DOM操作
2.1 节点编号、名称以及值
2.2 父节点、子节点和相邻节点
2.3 添加和删除节点
2.4 属性相关
2.5 DOM合集
2.6 DOM遍历
三、其他
3.1 事件
3.2 浏览器检测
3.3 三种弹窗方式
3.4 根据浏览器历史控制前进后退
3.5 重载页面的六种方式
3.6 修改当前页面URL但是不刷新页面
3.7 URI编码
3.8 窗口相关
一、基本类型
1.1 字符串
判断是否包含某个字符串
indexOf方法中,找到相关字符串会返回第一次出现的下标。没有找到就会返回-1,利用这个特性可以判断字符串是否存在。
console.log('Fine'.indexOf('in') !== -1) // true
把字符串按照一定规则分离成数组
下面是以空格为分割标志。
console.log('I seek you'.split(' ')) // ["I", "seek", "you"]
复制指定位置的字符串
传入的两个参数分别是开始的位置和结束的标记。看清楚,第二个参数本身的小标所表示的内容不会被复制,第二个参数是用来标记,这里是结束位置。
console.log('I seek you'.slice(2,6)) // seek
console.log('I seek you'.substring(2,6)) // seek
拼接字符串
console.log('I seek you'.concat(' too.')) // I seek you too.
查看字符串中的字符
console.log('I seek you'[0]) // I
console.log('I seek you'.charAt(0)) // I
1.2 对象
判断属性是自己的还是继承来的
使用in不能判断属性是自己的还是继承来的,使用hasOwnProperty可以。
var xiaoming = {
name: 'xiaoming'
}
使用in不能判断属性是自己的还是继承来的
---------------------------------
'name' in xiaoming // true
'toString' in xiaoming // true
---------------------------------
xiaoming.hasOwnProperty('name') // true
xiaoming.hasOwnProperty('toString') // false
判断对象是否可枚举
xiaoming.propertyIsEnumerable() // false
判断对象是另一个对象的原型
var People = function (name) {
this.name = name
}
var xiaoming = new People('xiaoming')
Human.prototype = monkey
monkey.isPrototypeOf(man)
1.3 原型
- __proto__是实例对象的属性
- prototype是构造函数的属性
- constructor指向构造函数
IE中不存在__proto__,推荐使用ES5的Object.getPropertyOf()访问。
typeof [].__proto__ // "object"
Object.getPrototypeOf([]) // [constructor: function, toString: function, toLocaleString: function, join: function, pop: function…]
[].constructor.prototype
原型继承
var People = function (name,age) {
this.name = name
this.age = age
}
xiaoming = People.prototype
xiaoming.constructor = xiaoming
1.4 常用数学方法
Math.PI // 3.141592653589793
Math.SQRT2 // 1.4142135623730951
Math.sqrt(2) // 1.4142135623730951
Math.E // 2.718281828459045
Math.pow(2,3) // 8
Math.random() * (10-2)+2 // 7.564475903879611 | 2-8之间的平均数
Math.LN2 // 0.6931471805599453
Math.floor(2.6) // 2 | 指定值的最小整数
Math.ceil(2.6) // 3 | 指定值最大整数
Math.round(2.6) // 3 | 去最靠近指定值的整数
Math.max() // 3
Math.min() // 2
Math.sin(90) // 0.8939966636005579
Math.cos(90) // -0.4480736161291702
二、DOM操作
2.1 节点编号、名称以及值
nodeType有12种,具体请见MDN
<div class="you">HELLO YOU</div>
var you = document.getElementsByClassName('you')[0]
you.nodeType // 1
you.nodeName // BIV
you.nodeValue // null
you.textContent // HELLO YOU
you.innerText // "HELLO YOU"
2.2 父节点、子节点和相邻节点
检查是否具有某个子节点
document.documentElement.hasChildNodes('body') // true
查看所有子节点
document.documentElement.childNodes // [head, text, body]
查看第一个子节点
document.documentElement.firstChild // <head>...</head>
访问父节点
document.documentElement.childNodes[0].parentNode
访问相邻节点
document.documentElement.children[0].previousSibling // null
document.documentElement.children[0].nextSibling // #text
2.3 添加和删除节点
<div class="you">HELLO YOU</div>
var you = document.getElementsByClassName('you')[0]
新建节点
var pTag = document.createElement('p')
var pVal = document.createTextNode('HELLO YOU')
pTag.appendChild(pVal) // <p>HELLO YOU</p>
添加节点
document.body.insertBefore(pTag,you)
document.body.replaceChild(you,pTag)
删除节点
document.body.removeChild(you)
克隆节点
true为深拷贝,会拷贝节点的内容。flase只拷贝空标签。
var newNodeFalse = document.body.cloneNode(true)
var newNodeFalse = document.body.cloneNode(false)
console.log(newNodeFalse) // <body>...</body>
console.log(newNodeFalse) // <body></body>
2.4 属性相关
<div class="you">HELLO YOU</div>
var you = document.getElementsByClassName('you')[0]
检查是否具有某个属性
you.hasAttributes('class') // true
获取具体属性
you.getAttribute('class') // "you"
you.attributes[0].nodeValue // "you"
you.attributes['class'].nodeValue // "you"
选择器
querySelector使用的是CSS选择器,返回单个节点。返回所有匹配的结果用querySelectorAll。
document.querySelector('.you')
document.querySelectorAll('.you') // [div.you]
批量添加样式
you.style.cssText = "color:red;font-size:200px;"
2.5 DOM合集
document.images
document.applets
document.links
document.anchors
document.forms
document.cookie
document.title
document.referrer
document.domain
2.6 DOM遍历
function walkDOM(n){
do {
console.log(n)
if(n.hasChildNodes()){
walkDOM(n.firstChild)
}
}
while (n=n.nextSibling)
}
walkDOM(document.body)
三、其他
3.1 事件
阻止冒泡
event.stopPropagation()
window.event.cancelBubble = true //IE
阻止默认事件
event.preventDefault()
return false // IE
拖动事件
触屏事件
这里有一个用canva实现的画图页面,触屏画图,实现过程可以直接看源码。。另外触屏事件的分析,请见伯乐在线。
touchstart
touchmove
touchend
touchleave
touchcancel
3.2 浏览器检测
用户代理可以被模拟,所以根据浏览器的不同特征来检测当前浏览器类型更加可靠。
if(window.addEventlistener) {
// code...
}
else if(){
// code...
}
3.3 三种弹窗方式
三种弹窗分别是提示框(alert),确认框(confirm)和交互框(prompt)。可以把确认和交互赋值给变量,变量会存储相应结果。
alert('Welcome To JS!')
var isLike = confirm('Do you like JS?')
var whyLike = prompt('Why do you like it.')
console.log(isLike) // true
console.log(whyLike) // Maybe...
3.4 根据浏览器历史控制前进后退
根据缓存的浏览器历史,可以控制前进、后退和跳转到指定历史记录。
window.history.forward() // 前进
window.history.back() // 后退
window.history.go(1) // 跳转
3.5 重载页面的六种方式
location.reload()
location.assign('/')
location.replace('/')
window.location.href = '/'
location = location
window.location.reload()
3.6 修改当前页面URL但是不刷新页面
history.pushState({a:1},'','hello')
3.7 URI编码
function decodeURI(url,params){
var url = url || 'http://www.cnblogs.com/bergwhite/'
var params = params || {name: 'berg', age: 22}
var query = []
for (param in params) {
query.push(param+'='+params[param])
}
return url+=query.join('&')
}
decodeURI() // "http://www.cnblogs.com/bergwhite/name=berg&age=22"
decodeURI('http://www.you.com/',{a:1,b:2}) // "http://www.you.com/a=1&b=2"
3.8 窗口相关
新窗口打开内容
window.open('http://www.baidu.com','zzzzzzzzzzzz','width=800px,height=300px,resizable=yes')
判断是否是高分辨率屏幕
window.devicePixelRatio // 1
感谢您的阅读。
《JavaScript面向对象编程指南(第2版)》读书笔记(二)的更多相关文章
- 《CSS世界》笔记二:盒模型四大家族
上一篇:<CSS世界>笔记一:流/元素/尺寸下一篇:<CSS世界>笔记三:内联元素与对齐 写在前面 在读<CSS世界>第四章之前,粗浅的认为盒模型无非是margin ...
- CSS揭秘读书笔记 (一)
CSS揭秘读书笔记 (一) 一.半透明边框 要想实现半透明边框可以使用border: border: 10px solid hsla(0,0%,100%,.5); background: ...
- 《你必须知道的.NET》读书笔记二:小OO有大原则
此篇已收录至<你必须知道的.Net>读书笔记目录贴,点击访问该目录可以获取更多内容. 一.单一职责原则 (1)核心思想:一个类最好只做一件事,只有一个引起它变化的原因 (2)常用模式:Fa ...
- spring揭秘 读书笔记 二 BeanFactory的对象注册与依赖绑定
本文是王福强所著<<spring揭秘>>一书的读书笔记 我们前面就说过,Spring的IoC容器时一个IoC Service Provider,而且IoC Service Pr ...
- ES6读书笔记(二)
前言 前段时间整理了ES6的读书笔记:<ES6读书笔记(一)>,现在为第二篇,本篇内容包括: 一.数组扩展 二.对象扩展 三.函数扩展 四.Set和Map数据结构 五.Reflect 本文 ...
- 《精通CSS》读书笔记(一)
最近新添16本书,目前开始看陈剑瓯翻译的<精通CSS——高级Web标准解决方案>(Andy Budd, CSS Mastery -- Advanced Web Standards Solu ...
- spring揭秘 读书笔记 二 BeanFactory的对象注冊与依赖绑定
本文是王福强所著<<spring揭秘>>一书的读书笔记 我们前面就说过,Spring的IoC容器时一个IoC Service Provider,并且IoC Service Pr ...
- 【记】《.net之美》之读书笔记(二) C#中的泛型
前言 上一篇读书笔记,很多小伙伴说这本书很不错,所以趁着国庆假期,继续我的读书之旅,来跟随书中作者一起温习并掌握第二章的内容吧. 一.理解泛型 1.为什么要使用泛型?-----通过使用泛型,可以极大地 ...
- Mastering Web Application Development with AngularJS 读书笔记(二)
第一章笔记 (二) 一.scopes的层级和事件系统(the eventing system) 在层级中管理的scopes可以被用做事件总线.AngularJS 允许我们去传播已经命名的事件用一种有效 ...
- how tomcat works 读书笔记(二)----------一个简单的servlet容器
app1 (建议读者在看本章之前,先看how tomcat works 读书笔记(一)----------一个简单的web服务器 http://blog.csdn.net/dlf123321/arti ...
随机推荐
- Java Reference 源码分析
@(Java)[Reference] Java Reference 源码分析 Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使 ...
- 在VM中给Linux安装Tool
1.导入tool 2.解压tool 3.打开终端,进入tool的目录,输入 ./XXXXXXX.pl 4.进入安装界面,不断回车即可
- 阿里云Linux启动tomcat并能外网访问
问题描述: 先描述一下我的心路历程吧,新买了阿里云服务器,由于需求不是很大,只是为了备案,所以买了个最低配的,而且是Windows server2012的.那现在需要做的是在这个乞丐版的server上 ...
- 读书笔记 effective c++ Item 36 永远不要重新定义继承而来的非虚函数
1. 为什么不要重新定义继承而来的非虚函数——实际论证 假设我告诉你一个类D public继承类B,在类B中定义了一个public成员函数mf.Mf的参数和返回类型并不重要,所以假设它们都是void. ...
- 在Windows下安装redis扩展和memcached扩展
一.php安装redis扩展 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本 2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...
- 网站防止SQL注入方法
方法:所有获取GET.POST变量都先进行过滤: 字符串-- htmlspecialchars(addslashes($string)) addslashes() 函数返回在预定义字符之前添加反斜杠 ...
- Access-简易进销存管理系统
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- 3.XML的格式化显示
使用CSS/XSLT格式化XML,可以使XML具有更加多彩的显示效果. 3.1 使用CSS格式化显示XML 使用CSS格式化XML只需要在XML中加上: <?xml-stylesheet typ ...
- ios ALAssetsLibrary简单的使用
关于ALAssetsLibrary的简单使用有两个方面: 第一:存储图片/视频方法如下: // With a UIImage, the API user can use -[UIImage CGIma ...
- vue项目目录介绍
Vue项目目录 初始化项目 vue init webpack []projectname] cd [projectname] npm install vue run dev 目录树 +---build ...