标签:des style blog http io ar color os 使用
1:throw new TypeError("test");
2:var list=Object(this)
3:var len = list.length >>> 0;
一:
(function(win){ ... window.bs_patch = (function(){ .... })();
var patch = window.bs_patch;
window.bs_patches = function(impl,obj){}; var patches=windows.bs_patches;
patches({ ..of,isArray.. },Array); patches({ ..entries,every,fill,fliter,find,findIndex,keys,forEach,copyWithin,indexOf,lastIndexOf,map,reduce,reduceRight,some.. },Array.prototype); patch((function(){ ..unshift.. })(),Array.prototype,"unshift",isIE68); patch((function(){ ..join.. })(),Array.prototype,"join",isIE68); patches({ ..now.. },Date); patch((function(){ .... })(),Date,‘parse‘,isIE); patches({ .... },Date.prototype); patch(function(oThis){ .... },Function.prototype,‘bind‘); patch(function(argv){ .._Iterator.. },window,"Iterator"); patch({},window,‘JSON‘); patches({},window.JSON); patch((function(){ .._Map.. })(),window,‘Map‘); patches({ ..delete,clear,entries,forEach,get,has,keys,set,values.. },Map.prototype); patches({ ..acosh,asinh,atanh,cbrt,clz32,cosh,expml,fround,hypot,imul,log10,log1p,log2,sign,sinh,tanh,trunc.. },Math); patches({..epsilon,MAX_VALUE,MIN_VALUE,MAX_SAFE_VALUE,MIN_SAFE_VALUE,NEGATIVE_INFINITY,POSITIVE_INFINITY,"NaN",isFInite,isInteger,isNaA,parseFloat,parseInt,toInteger..},Number); patches({ ..setPrototypeOf,is,create,getOwnPropertyNames,getPrototypeOf.. },Object); patch((function(){ .... })(),Object,‘keys‘); patch((function(){ .._Proxy.. })(),window,‘Proxy‘); patch((function(){ .._Set,patches({ ..delete,clear,entries,forEach,has,add,values..}),_Set.prototype.. })(),window,‘Set‘); patches({ ..codePointAt,contains,endsWith,repeat,startsWith,trim,trimLeft,trimRight.. },String.prototype); patches({ ..fromCodePoint.. },String); patch(function(){ .._Promise(deferred,resolve,reject,all,race,).. }(),window,‘Promise‘); })(window); })
1.(function(){})()
1.1:匿名执行函数,()的优先级最高,相当于var x=function(){},x();
1.2: Object.defineProperty
YY = YY || {};// 定义全局变量,然后在里面加方法
(function(){
YY.Lang = function(){
isUndefined:function(o){ return typeof o === ‘undefuned‘ },
isString:function(o){ return typeof o === ‘string‘ }
}
})();
alert(YY.Lang.isString(‘test me‘));
1.3:(function(){ var a=2;alert(a); })()和var a=2;alert(a);的区别是前者调用完之后a会释放空间,而后者不会。
二:
window.bs_patch = (function() { // IE8 window.hasOwnProperty window.hasOwnProperty = window.hasOwnProperty || Object.prototype.hasOwnProperty; if(Object.defineProperty && !isIE68) { return function(impl, obj, method, condition) { if((method && typeof obj[method] == "undefined") || condition) { Object.defineProperty(obj, method, { //IE678提示找不到defineProperty方法 enumerable: false, configurable: true, writable: false, value: impl }); } }; }else { return function(impl, obj, method, condition) { if((method && typeof obj[method] == "undefined") || condition) { obj[method] = impl; } }; } })();
var patch = window.bs_patch;
window.bs_patches = function(impl, obj) {
for(var key in impl) {
if(impl !== window && impl.hasOwnProperty(key)) {
patch(impl[key], obj, key);
}
}
}
var patches = window.bs_patches;
2.1: Object.defineProperty
var o = window; // IE8下window可以打印出来,如果是{}则打印不出来 Object.defineProperty(o,‘abc‘,{value:123}); alert(o.abc);
2.1.1 :Object.defineProperty(object, propertyname, descriptor)
2.1.2:
Internet Explorer 9 标准模式和 Internet Explorer 10 标准模式以及 Windows 应用商店 应用程序都支持所有功能。
2.2:window.hasOwnProperty = window.hasOwnProperty || Object.prototype.hasOwnProperty;
function A(name){
this.name=name; this.showAge=function(){}; } A.prototype.showName = function(){ alert(this.name); } var B=new A("yuyu"); alert(B.hasOwnProperty("showAge")); // true alert(B.hasOwnProperty("showName")); // false
alert(A.hasOwnProperty("showAge")); // false
alert(A.hasOwnProperty("name")); // true alert(A.prototype.hasOwnProperty("showAge")); //false alert(A.prototype.hasOwnProperty("showName")); //true alert(A.prototype.isPrototypeOf(B)); //true
alert(B.prototype.hasOwnProperty("showName")); // 会报错,只有原型有prototype属性
2.3:hasOwnProperty 无法检查该对象的原型链中是否具有该属性
三:call和apply
function add(a,b){ console.log(a+b); } function sub(a,b){ console.log(a-b); } function A(name,age){ this.name=name; this.age=age; this.showName=function(){ console.log(this.name) } } function B(name){ this.name=name; this.showText=function(){ console.log("B-text"); } } var A1=new A(); var B1=new B(); A1.showName.call(B1); // 输出 undefined function C(name){ A.call(this,name); // 即用call就可以继承A的方法,还可同时继承多个 } var C1=new C("yuyu"); C1.showName(); // 输出yuyu // 对象的继承有三种方法 apply call extend:prototype.js的实现方式是: Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; } function D(name,age){ A.apply(this,arguments); // apply和call的区别是:apply能将参数依次传进A中。且参数name和age是和A中对应的。如果不对应,则应该是A.call(this,age,name) B.apply(this,arguments); } var D1=new D("D",24); D1.showName(); // 输出D console.log(D1.age); // 输出24 D1.showText(); // 输出B-text*/ // apply的两个实例;call 的第二个参数可以是任意类型,而apply的第二个参数必须是数组,也可以是arguments var arr1=[1,6,3,4],arr2=[5,9,0];//获取arr中的最大数;Math.max后面可以接任意个参数,最后返回所有参数中的最大值。Math.max(arr);写法错误 console.log( Math.max.apply(null,arr1) ); // 输出6 Array.prototype.push.apply(arr2,arr1); console.log(arr2); // 输出 [5, 9, 0, 1, 6, 3, 4]
patches({ ‘of‘:function() { return Array.prototype.slice.call(arguments); //相当于arguments.slice();将arguments转换为数组 }, isArray:function(arg) { return Object.prototype.toString.call(arg) === ‘[object Array]‘; } }, Array);
3.1:
/* typeof 只能判断出"number" "string" " undefined" "boolean" ‘object" 对对象,数组,函数统一返回"object" */ console.log(typeof(123)); //number console.log(typeof("123")); //string console.log(typeof(undefined)); //undefined console.log(typeof(true)); //boolean console.log(typeof({})); //object console.log(typeof([])); //object console.log(typeof(function(){})); //function /* instanceof 可以判断typeof 不能判断出的数组,对象,函数 */ var a={},b=[],c=function(){}; console.log(a instanceof Object); // a b c 都为true console.log(c instanceof Array); //只有b为true console.log(c instanceof Function); // 只有c为true console.log( (a instanceof Object) && !(a instanceof Function) && !(a instanceof Array));// 判断a对象 true console.log((b instanceof Object) && (b instanceof Array)); // 判断为数组 console.log((c instanceof Object)&&(c instanceof Function)); // 判断为函数 /* Object.prototype.toString.call() 可以判断所有的值 */ console.log(Object.prototype.toString.call(123)); //[objcet Number] console.log(Object.prototype.toString.call(‘123‘)); //[object String] console.log(Object.prototype.toString.call(undefined)); //[object Undefined] console.log(Object.prototype.toString.call(true)); //[object Boolean] console.log(Object.prototype.toString.call({})); //[object Object] console.log(Object.prototype.toString.call([])); //[object Array] console.log(Object.prototype.toString.call(function(){})); //[object Function]
3.2:Array.prototype.slice.call(arguments,0);将参数转换成真正的数组,call的作用是改变this的指向,就相当于arguments调用了,slice这个方法。0就是start=0,end没指定,所以返回整个arguments
/* slice 经常用来将 array-like 对象转换为 true array。在一些框架中会经常有这种用法。 */ var s="123456"; var b=[1,2,3,4,5] console.log(s.slice(0,4)); //1234 console.log(b.slice(0,4)); //[1, 2, 3, 4]
3.2:为什么不用arguments.slice(0);slice要求有leng属性,arguments有length,但是没有slice方法,因为Array是类名,而不是对象名,所以不能直接用Array.slice
四:
patches({ entries:function() { return Iterator(this); }, every:function(fun) { ‘use strict‘; if (this === void 0 || this === null) //void 0=undefined throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== ‘function‘) throw new TypeError(); var thisArg = arguments.length >= 2 ? arguments[1] : void 0; for (var i = 0; i < len; i++) { if (i in t && !fun.call(thisArg, t[i], i, t)) return false; } return true; }, fill:function(value) { var O = Object(this); var len = parseInt(O.length); var start = arguments[1]; var relativeStart = parseInt(start) || 0; var k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len); var end = arguments[2]; var relativeEnd = end === undefined ? len : (parseInt(end) || 0); var final = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len); for (; k < final; k++) { O[k] = value; } return O; }, filter:function(fun) { "use strict"; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); var res = []; var thisArg = arguments.length >= 2 ? arguments[1] : void 0; for (var i = 0; i < len; i++) { if (i in t) { var val = t[i]; if (fun.call(thisArg, val, i, t)) res.push(val); } } return res; }, find:function(filter, context) { var index = this.findIndex(filter, context); if(index >= 0) { return this[index]; }else { return undefined; } }, findIndex:function(filter, context) { if(typeof filter != ‘function‘) { throw new TypeError(‘filter must be a function‘); } var list = Object(this); var len = list.length >>> 0; var value; for(var i=0;i<len;i++) { if(i in list) { value = list[i]; if(filter.call(context, value, i, list)) { return i; } } } return -1; }, keys:function() { var ite = Iterator(this); ite.next = function() { var value = this.iterator[this.current++]; if(this.current > this.length) { throw new Error("stop iterate"); } return { value:value[0], done:this.current >= this.length }; }; return ite; }, forEach:function(callback, thisArgs) { var t, k; if(this == null) { throw new TypeError(‘this is null or undefined‘); } var o = Object(this); var len = o.length >>> 0; if(typeof callback != ‘function‘) { throw new TypeError(‘callback is not a function‘); } if(thisArgs) t = thisArgs; k = 0; while(k < len) { var kValue; if(k in o) { kValue = o[k]; callback.call(t, kValue, k, o); } k++; } }, copyWithin:function(target, start) { var O = Object(this); var len = parseInt(O.length); var relativeTarget = parseInt(target); var to = relativeTarget < 0 ? Math.max(len + relativeTarget, 0) : Math.min(relativeTarget, len); var relativeStart = parseInt(start); var from = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len); var end = arguments[2]; var relativeEnd = end === undefined ? len : parseInt(end); var final = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len); var count = Math.min(final - from, len - to); if (from < to && to < (from + count)) { from = from + count - 1; to = to + count - 1; while (count > 0) { if ( from in O) O[to] = O[from]; else delete O[to]; from--; to--; count--; } } else { while (count > 0) { if ( from in O) O[to] = O[from]; else delete O[to]; from++; to++; count--; } } return O; }, indexOf: function (searchElement, fromIndex) { if (this === undefined || this === null) { throw new TypeError(‘"this" is null or not defined‘); } var length = this.length >>> 0; fromIndex = +fromIndex || 0; if (Math.abs(fromIndex) === Infinity) { fromIndex = 0; } if (fromIndex < 0) { fromIndex += length; if (fromIndex < 0) { fromIndex = 0; } } for (; fromIndex < length; fromIndex++) { if (this[fromIndex] === searchElement && fromIndex in this) { return fromIndex; } } return -1; }, lastIndexOf: function(searchElement) { ‘use strict‘; if (this === void 0 || this === null) { throw new TypeError(); } var n, k, t = Object(this), len = t.length >>> 0; if (len === 0) { return -1; } n = len - 1; if (arguments.length > 1) { n = Number(arguments[1]); if (n != n) { n = 0; } else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) { n = (n > 0 || -1) * Math.floor(Math.abs(n)); } } for (k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); k >= 0; k--) { if (k in t && t[k] === searchElement) { return k; } } return -1; }, map:function(callback, thisArg) { var T, A, k; if (this == null) { throw new TypeError(" this is null or not defined"); } var O = Object(this); var len = O.length >>> 0; if (typeof callback !== "function") { throw new TypeError(callback + " is not a function"); } if (thisArg) { T = thisArg; } A = new Array(len); k = 0; while (k < len) { var kValue, mappedValue; if (k in O) { var Pk = k.toString(); kValue = O[Pk]; mappedValue = callback.call(T, kValue, k, O); A[Pk] = mappedValue; } k++; } return A; }, reduce:function(callback) { ‘use strict‘; if (null === this || ‘undefined‘ === typeof this) { throw new TypeError(‘Array.prototype.reduce called on null or undefined‘); } if (‘function‘ !== typeof callback) { throw new TypeError(callback + ‘ is not a function‘); } var t = Object(this), len = t.length >>> 0, k = 0, value; if (arguments.length >= 2) { value = arguments[1]; } else { while (k < len && !k in t) k++; if (k >= len) throw new TypeError(‘Reduce of empty array with no initial value‘); value = t[ k++ ]; } for (; k < len; k++) { if (k in t) { value = callback(value, t[k], k, t); } } return value; }, reduceRight:function(callback) { ‘use strict‘; if (null === this || ‘undefined‘ === typeof this) { throw new TypeError(‘Array.prototype.reduce called on null or undefined‘); } if (‘function‘ !== typeof callback) { throw new TypeError(callback + ‘ is not a function‘); } var t = Object(this), len = t.length >>> 0, k = len - 1, value; if (arguments.length >= 2) { value = arguments[1]; } else { while (k >= 0 && !k in t) k--; if (k < 0) throw new TypeError(‘Reduce of empty array with no initial value‘); value = t[ k-- ]; } for (; k >= 0; k--) { if (k in t) { value = callback(value, t[k], k, t); } } return value; }, some:function(fun) { ‘use strict‘; if (this === void 0 || this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== ‘function‘) throw new TypeError(); var thisArg = arguments.length >= 2 ? arguments[1] : void 0; for (var i = 0; i < len; i++) { if (i in t && fun.call(thisArg, t[i], i, t)) return true; } return false; } }, Array.prototype);
4.1: ‘use strict‘
"use strict"; var num =012; alert(num);// IE678下还是能打印出来为10,chrome,ff下没有反应
4.1.1:如果给JavaScript代码标志为“严格模式”,则其中运行的所有代码都必然是严格模式下的。IE6,7,8,9均不支持严格模式。
4.1.2:位置:必须在全局代码的开始处加入。在eval代码开始处加入。在函数声明代码开始处加入。在new Function()所传入的body参数块开始加入。
4.1.3:
.在非严格模式下,可以使用0(零)开头前缀声明8进制。但是在严格模式下,会产生错误。
.在代码中不能使用一些扩展的保留字:implements,interface,let,package,private,public,static,yield
.with语句也不能使用。
.不能声明或重写eval和arguments两个标识符。
.不能用delete 删除显式声明的标识符,名称或具名函数。
.caller,callee,在严格模式下就不能使用的
4.2:this === void 0
4.2.1:void(0)的含义: void 运算符对表达式求值,并返回 undefined。 javascrīpt:void (expression)或者 javascrīpt:void expression;计算expression;
4.2.2: <A HREF="javascrīpt:void(0)"> <A HREF="javascrīpt:void(document.form.submit())">与href=#的区别;#,包含了一个位置信息,默认的锚点是#top
4.2.3:if (this === void 0 || this === null) undefined和null的区别:
1:undefined申明的变量没有被初始化时;var a;
var a; console.log(a == undefined); // true console.log(document.getElementById(‘b‘) == null); // true console.log(typeof undefined); // undefined console.log(typeof null); // object js的一个错误,被认为null即是一个不存在的对象的占位符 console.log(null == undefined); //true EMCAscript认为undefined是从null派生出来的,定义为相等 console.log(null === undefined); //false 判断是否相等 console.log(typeof null == typeof undefined); //false 判断是否相等 console.log(null + 123); // 123 null参加运算,自动算为0 console.log(b == null); // Uncaught ReferenceError: b is not defined console.log(b == undefined); // Uncaught ReferenceError: b is not defined
4.3:throw new TypeError() Error对象
try{ abc }catch(e){ for (var p in e){ document.writeln(p+‘=‘+e[p]+";"); } }
// IE下输出name=TypeError; message=‘abc‘ 未定义; number=-2146823279; description=‘abc‘ 未定义;用var errCode = err.number& x0FFFF; 以到http://msdn.microsoft.com/en-us/library/1dk3k160(VS.85).aspx微软msdn上查找此错误码对应的。5009对应的错误是Undefined identifier。
// ff下输出fileName=file:///D:/1-study1/test11.html; lineNumber=16; columnNumber=5;
// chrome下无输出
4.3.1:捕获异常下Error对象的属性有name(错误名),number(错误号),description(描述),message(多同description),ff下有fileName...;
.EvalError: 错误发生在eval()中
.SyntaxError: 语法错误,错误发生在eval()中,因为其它点发生SyntaxError会无法通过解释器
.RangeError: 数值超出范围
.ReferenceError: 引用不可用
.TypeError: 变量类型不是预期的
.URIError: 错误发生在encodeURI()或decodeURI()中
4.3.2:抛出异常:
4.4:object(this);Object对象
4.4.1:提供所有javascript对象共有的功能。obj = new Object(x);如果x不是对象,则返回未修改的对象,如果x是null,undefined或者未提供,则创建无内容的对象。
4.4.2:属性:_proto_Property(为对象指定原型);constructor(指定创建一个对象的函数),prototype(为对象的类型返回原型的引用)
4.4.3:函数:create,defineProperties(将一个或者多个添加或修改到对象);defineProperty,freeze(禁止添加),getOwnPropertyDescriptor(获得属性定义)
getOwnPropertyNames(获得对象的属性和方法名称);getPrototypeOf(返回对象的原型),isExtensible(是否可像对象添加属性),isSealed,keys(返回对象可枚举属性和方法的名称),preventExpression(阻止向对象添加新属性);seal(阻止修改现有属性的特效,并阻止添加新属性)
4.4.4:方法:hasOwnProperty,isPrototypeOf,propertyEnumerable,toLocaleString,toString,valueOf
4.4.5:Object(this)
var x; x={"yj":123,"er":2} //Object {yj: 123, er: 2} x=undefined; // Object {} x=null; //Object {} x=function a(){}; //function a(){} x=‘123‘; //String {0: "1", 1: "2", 2: "3", length: 3, [[PrimitiveValue]]: "123"} x=123; //Number {[[PrimitiveValue]]: 123} x=false; //Boolean {[[PrimitiveValue]]: false} x= Object(x); console.log(x);
4.5:t.length >>> 0右移运算符:>>> 运算符将 expression1 的位右移 expression2 中指定的位数。 用零填充右移后左边空出的位。 右移的位被丢弃。 例如:变量 temp 具有初始值 -14(二进制补码 11111111 11111111 11111111 11110010)。 其右移两位后,值等于 1073741820(即二进制的 00111111 11111111 11111111 11111100)。
4.6:fromIndex = +fromIndex || 0; 相当于:fromIndex = fromIndex || 0; +号不起作用。
4.7: else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) ---- (1/0):Infinity ;-(1 / 0):-Infinity
4.8:Date
4.8.1:Date.parse(‘Jul 8,2005‘);返回与1970.1.1午夜之间的毫秒数;1s=1000ms
4.8.2:new Date().getTimezoneOffset()
/60 输出-8;getTimezoneOffset() 方法返回的是本地时间与 GMT 时间或 UTC 时间之间相差的分钟数。实际上,该函数告诉我们运行 JavaScript 代码的时区,以及指定的时间是否是夏令时。
4.8.3:Date
.
UTC
(
year
,
month
,
day
,
hours
,
minutes
,
seconds
,
microseconds
)
;同parse,返回毫秒数。
Date
.
UTC
(
1980
,
12
,
17
);
4.8.4:toJSON()方法用來取得日期物件中的JSON格式的日期字串,JSON格式的日期字串跟 ISO-8601標準: YYYY-MM-DDTHH:mm:ss.sssZ是一樣的 console.log(new Date().toJSON()); // 2014-11-17T10:13:07.328Z
4.8.5:13.67.tofixed(1);== 13.7:四舍五入为以为小数。
4.8.6:arr.concat() 方法用于连接两个或多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
4.9:JSON
// JavaScript 对象表示法(JavaScript Object Notation)。JSON 是存储和交换文本信息的语法。类似 XML。JSON 比 XML 更小、更快,更易解析。JSON 数据可使用 AJAX 进行传输 // 用eval() 方法进行解析,JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 JavaScript 对象,然后在网页中使用该数据。 // 格式:数据在名称/值对中,数据由逗号分隔,花括号保存对象,方括号保存数组 // { "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] } // var employees = [ { "firstName":"Bill" , "lastName":"Gates" }, { "firstName":"George" , "lastName":"Bush" }, { "firstName":"Thomas" , "lastName": "Carter" } ]; var txt = ‘{ "employees" : [{ "A":"A1"},{ "A":"A2","B":"B2"}]}‘; var obj = eval ("(" + txt + ")"); console.log(obj.employees[0].A); //输出:A1 // 这两个方法分别用于stringify()把JavaScript对象序列化为JSON字符串,parse()把JSON字符串解析为原生JavaScript var A = {name:"yuyu",age:24,a:undefined,func1:function(){}}; var B=["yuyu","yuyu2",1]; var C=[1,2]; function fun1(key,value){ return value.toString().toUpperCase(); } console.log(A); // 输出:Object {name: "yuyu", age: 24, a: undefined, func1: function} console.log(JSON.stringify(A)); // 输出 {"name":"yuyu","age":24},所有函数,值为undefined的任何属性也都会被跳过,a和func1不会输出 console.log(JSON.parse(JSON.stringify(A))); // 输出 Object {name: "yuyu", age: 24} console.log(JSON.stringify(B)); //输出:["yuyu","yuyu2",1] console.log(JSON.stringify(B,fun1)); //输出:"YUYU,YUYU2,1";第二个参数是function时,对B进行func1 console.log(JSON.stringify(A,C)); console.log(JSON.stringify(B,C,10));// 第二个为数组时对输出没有影响,第三个参数表示缩进的格数 /* [ "yuyu", "yuyu2", 1 ]*/
// slice截取start到end的数组,不包含end,返回截取的数组,splice删除start开始的几个数,返回删除掉的数 var arr1=[1,2,3,4,5,6]; console.log(arr1.slice(1,4)); //输出:[2, 3, 4] console.log(arr1); // 输出:[1, 2, 3, 4, 5, 6] console.log(arr1.splice(1,4)); // 输出:[2, 3, 4, 5] console.log(arr1); // 输出:[1, 6]
标签:des style blog http io ar color os 使用
原文地址:http://www.cnblogs.com/startlove/p/4015317.html