/*
var o={x:1,y:2,z:3};//字面量方法创建对象
var obj=Object.create(o);//create 方法创建对象
obj.a=3;
for(key in obj){
console.log(key.value);
}
*/
/* var obj=new Object();//原型连方法创建对象
obj.x=3;
obj.y=4;
for(key in obj){
console.log(obj['x']);
}
/*
*!/
var obj={x:1,y:2};
/!*Object.preventExtensions(obj);//不可增加属性*!/
console.log(Object.getOwnPropertyDescriptor(obj,'x'));
/!*Object.seal(obj);*!/
console.log(Object.getOwnPropertyDescriptor(obj,'x'));
/!* Object.freeze(obj);
console.log(Object.getOwnPropertyDescriptor(obj,'x'));*!/
Object.defineProperty(obj,'a',{
writable:true,
enumerable:true,
configurable:true,
value:6
});
console.log(Object.getOwnPropertyDescriptor(obj,'a'));*/
/* var obj={
x:1,
y:2,
o:{
o1:1,
o2:2,
toJSON: function () {
return this.o1+this.o2;
}
}
};
console.log(JSON.stringify(obj));*/
/*
var arr=Array(true,false,1,'hi');
Array.prototype.x='what';//数组原型链上添加属性 在for in 迭代中会输出原型链上的属性
for(i in arr){
console.log(arr[i]);
}
*/
/*var arr=Array(1);
console.log(arr);*/
/*var arr=[1,2,3];
console.log(arr.join('_'));//[1_2_3]
function repeatString(str,n){
return new Array(n+1).join(str);
}
console.log(repeatString('a',4));//[aaaa]*/
/*var arr=[1,2,3,4,5,6,7,8,9,10];//数组筛选
var q=arr.filter(function (x,index) {
return index%3===0||x>=8;
});
console.log(q);
console.log(arr)//筛选后不改变原来的数组*/
/*var arr=[1,2,3];
var sum=arr.reduce(function(x,y){
return x+y;
},0);//0 作为最开始的X的值 0+1的值 作为下一次X的值 两两相加 原数组未被修改 reduceRight 从数组右边开始遍历
console.log(sum);*/
//indexOf(x ,y ) 从左向右查找 x:要查找的目标 y:从什么位置开始找支持负数 代表从右边开始找
// lastindexOf(x,y)从右向左查找
//判断是否是数组的四种方式
//1.Array.isArray([arr1]); 2.[arr1]instanceof Array; 3.({}).toString.apply([arr1])==='[object Array]';
// 4.[].constructor===Array;
//数组增加元素 arr.push() 从后面插入 arr.unshift() 从前面插入 arr.pop()从后面删除 arr.shift()从前面删除
/*var o={prop:37};
function independent(){
return this.prop;
}
o.f=independent;
console.log(o.f());//37*/
/*this.x=9;//this 指向window
var module={
x:81,
getX:function(){return this.x}
};
console.log( module.getX());//81
var getx=module.getX;
console.log(getx());//9
var bindgetx=getx.bind(module);//绑定到module 改变this 指向module
console.log(bindgetx());//81*/
//闭包概念理解
/*
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
var temp=object.getNameFunc();//this.name 没有被执行
var name=temp();//this.name 此时被执行 this的上下文为全局 window
alert(name);
//alert(object.getNameFunc()());//The window 因为当一个函数作为函数而不是方法来调用的时候 this 指向全局变量
*/
/*var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
var that = this;
return function(){
return that.name;
};
}
};
var temp=object.getNameFunc();//此时的执行that=this this 指向object
var name=temp()// that.name==object.name;
alert(name);
/!*alert(object.getNameFunc()());*!/*/
/*跨域取值*/
/*
var x=10;
function fn(){
console.log(x);
}
function show(f) {
var x = 20;
!function () {
f();
}();
}
show(fn);//输出 10 作用域链 x为自由变量 当调用fn 函数时 需要到创建该函数的作用域中取值 fn 是在全局作用域下纯创建的
*/
var max=10;
fn=function(x){
if(x>max)
console.log(x);
}
!function(f){
var max=20;
f(15);
}(fn);