@windchimes
2018-02-28T07:45:55.000000Z
字数 5142
阅读 251
小知识点
instanceof 判断类型
let obj = $('');if (obj instanceof jQuery) {console.log('jQuery')}else {dom}
Function function
function是Function的一个实例,Function是构造函数,是个类
function = new Function(arg1,arg2,...argN,function)
{} []的布尔值都是true,而‘’的空字符串是fasle
== === 两个等号进行类型转换,string=>number boolean=>number
NaN不和任何一个相等,全等更不相等
null == undefined //true
null === undefined //false
数组方法splice
形成新的数组,接收3个参数,index,length,item
定时闹钟多个地方调用时,每次调用前一定要清除一遍,否则会乱掉
IE用attachEvent 只支持事件冒泡.冒泡的终点是window对象,IE8除外
W3C用addEventListener('click',function(){},true),三个参数,true捕获,false冒泡
map方法
不改变原数组,生成新的数组,接收3个参数,分别是currentvalue index array, 操作的值、索引、原数组
var kvArray = [{key: 1, value: 10},{key: 2, value: 20},{key: 3, value: 30}];var reformattedArray = kvArray.map(function(obj) {var rObj = {};rObj[obj.key] = obj.value;return rObj;});
[{1:10},{2:20},{3:30}]
函数声明会覆盖变量声明,但不会覆盖变量赋值。
var a = 1; function a() { return true; } console.log(a); //1
var a; function a() { return true; } console.log(a); // function
很不错的js面试题
https://zhuanlan.zhihu.com/p/28428367
js继承:
原型链继承
构造器函数继承
组合继承
寄生组合继承
讲解链接:http://www.cnblogs.com/ayqy/p/4471638.html
原型链继承
注意:1)不能使用对象字面量创建原型方法,因为这样会创建出一个object的实例,而不是原型的实例,例如Cat中不能这么写:
Cat.prototype = {getName: function() {}} //错误
2)引用类型的原型属性会被所有的实例共享,一旦某个实例更改了引用类型的属性,则全部实例的属性值都会被改变
function Animal(name,food) {this.name = name;this.food = food;this.eat = function () {console.log(this.name + '喜欢吃' + this.food)}}function Cat(){ }Cat.prototype = new Animal('猫','小鱼干');var cat = new Cat();cat.eat();
构造器继承
function Animal(name,food) {this.name = name;this.food = food;this.eat = function() {console.log(this.name + '爱吃' +this.food)}}function Cat(name,food){Animal.call(this,name,food);}var cat = new Cat('猫','大鱼干')cat.eat();
组合继承
function Animal(name,food) {this.name = name;this.food = food;this.eat = function() {console.log(this.name + '爱吃' +this.food)}}function Cat(name,food){Animal.call(this,name,food);}Cat.prototype = new Animal();var cat = new Cat('猫','大鱼干')cat.eat();
寄生组合继承
// 切掉了原型对象上多余的那份父类实例属性function beget(obj) {var F = function () {};F.prototype = obj;return new F();}function Super(){// 只在此处声明基本属性和引用属性this.val = 1;this.arr = [1];}// 在此处声明函数Super.prototype.fun1 = function(){};Super.prototype.fun2 = function(){};//Super.prototype.fun3...function Sub(){Super.call(this); // 核心// ...}var pro = beget(Super.prototype);pro.constructor = Sub;Sub.prototype = pro;
取出字符串中重复次数最多的字符和重复次数
思路:1)json对象,遍历字符串,不同的字符串放入json中不同的数组,找出长度最长的数组就ok
2)新建json对象,不重复为1,重复则++,最后拿出json里value最大的值,value是次数,对应的key就是那个重复次数最多的字符
3)正则表达式 /(\w)\1+/g
let str = 'dafsdfasfaeagsdfasfawefasdfas',json = {},i = 0,max = 0,val = '';for (; i < str.length; i++) {let key = str[i];if (!json[key]) {json[key] = 1;} else {json[key]++;}}console.log(json);for (var key in json) {if (json[key] > max) {max = json[key];val = key;}}console.log(max);console.log(val);
实现类似getElementsByClassName的功能
function queryClassName(node, name) {var starts = '(^|[ \n\r\t\f])', //空格字符 |或ends = '([ \n\r\t\f]|$)';var array = [],regex = new RegExp(starts + name + ends),elements = node.getElementsByTagName("*"),length = elements.length,i = 0,element;while (i < length) {element = elements[i];if (regex.test(element.className)) {array.push(element);}i += 1;}
深拷贝与浅拷贝
jQuery.extend可以实现深拷贝
JSON.parse(JSON.stringify())
JSON.parse()和JSON.stringify()能正确处理的对象只有Number、String、Array等能够被json表示的数据结构,因此函数这种不能被json表示的类型将不能被正确处理
var target = {a: 1, b: 1, c: {ca: 11, cb: 12, cc: 13}};var targetCopy = JSON.parse(JSON.stringify(target));targetCopy.a = 2;targetCopy.c.ca = 21;console.log(target); // {a: 1, b: 1, c: {ca: 11, cb: 12, cc: 13}}console.log(targetCopy); // {a: 2, b: 1, c: {ca: 21, cb: 12, cc: 13}}console.log(target === targetCopy); // false
animation transfrom transition区别
animation:动画效果,不需要触发,可以直接在css中执行
transform: 静态效果,指定形状之类的 transform: scale(1,1);
transition: 需要特定的触发事件,只有开始和结束两个状态
animate:jquery方法,兼容性较好,animate(css,speed,callback)
其中css需要具体的数值,比如margin-top:20px
this 讲解:http://web.jobbole.com/85198/
var o = {a:10,b:{a:12,fn:function(){console.log(this.a); //undefinedconsole.log(this); //window}}}var j = o.b.fn;j();
this指向的永远是最后调用它的对象,也就是看它执行的时候是谁来调用的,虽然fn是被b所引用,但是最后执行的时候是window,所以undefined
this和return共用时要注意:return时是对象则为undefined,因为此时this指向的是那个返回的空对象,如果返回值不是对象那么this还是指向函数的实例
function fn(){this.user = '追梦子';return {};}var a = new fn;console.log(a.user); //undefinedfunction fn(){this.user = '追梦子';return function(){};}var a = new fn;console.log(a.user); //undefinedfunction fn(){this.user = '追梦子';return 1;}var a = new fn;console.log(a.user); //追梦子function fn(){this.user = '追梦子';return undefined;}var a = new fn;console.log(a.user); //追梦子function fn(){this.user = '追梦子';return null;}var a = new fn;console.log(a.user); //追梦子
location.reload()重新加载页面所有资源,不读取缓存 location.href()会读取本地的存储资源
function Foo() {getName = function () {alert(1);};return this;}Foo.getName = function () {alert(2);};Foo.prototype.getName = function () {alert(3);}var getName = function () {alert(4);};function getName() {alert(5);}Foo.getName(); // 2getName(); // 4Foo().getName(); // 1getName(); // 1new Foo.getName(); // 2new Foo().getName(); // 3new new Foo().getName(); // 3
教程讲解:https://www.cnblogs.com/xxcanghai/p/5189353.html
proto prototype
每个对象都会有proto来标识自己所继承的原型,可是只有构造函数才有prototype属性
当一个函数变成构造函数的时候,它的prototype指针指向的是原型对象
实例对象的proto指向了原型对象
var cat = new Cat();
cat是Cat的一个实例对象,
cat.proto == Cat.prototype;
Cat new一个实例的时候,就变成了构造函数,此时Cat拥有了prototype属性,cat拥有了proto属性,cat是Cat的一个实例
http://yijiebuyi.com/blog/7193e9e04fa01c4d7c05ec5b29494d9d.html
Image缓存图像原理:Image()构造函数会创建一个屏幕外图片对象,之后将该对象的src属性指向期望的url,由于图片元素并没有添加到文档中所以是不可见的,但是实际浏览器已经将其加载完成并缓存起来
error是不能冒泡的,但是可以被捕获,使用addEventListener(true)事件