链式操作
var obj = {
morning:function(){
console.log('早上');
return this;
},
noon:function(){
console.log('中午');
return this;
},
night:function () {
console.log('晚上');
return this;
}
}
obj.morning().noon().night();//早上 中午 晚上
对象枚举
var arr = [1, 2, 3, 4, 5];
var car = {
brand: 'Benz',
color: 'red',
}
for (var key in car) {
//console.log(car.key);//undefined;
// car.key => car['key'];//报错
console.log(car[key])//
}
for (var key in arr) {
console.log(arr[key]);//1,2,3,4,5
console.log(key);// 0,1,2,3,4
}
hanOwnProperty
- 对象自身属性中是否包含指定的属性,有返回true 没有返回false
- for in 遍历会遍历整个对象包括原型上的属性 ```javascript // 申明构造函数Car。 function Car() { this.brand = ‘Benz’; this.color = ‘red’; this.displacement = ‘3.0’; }
//给Car的prototype重新赋值 Car.prototype = { lang: 5, width: 2 } // 实例化构造函数 var car = new Car();
//for in 循环实例化对象 for (var k in car) { console.log(car[k]);//此时会输出原型链上的lang 和width; } for(var k in car){ //判断car中是否包含k。 if (car.hasOwnProperty(k)) { console.log(car[k]);//此时没有输出原型的属性。 } }
<br />
<a name="X2Dw6"></a>
## instanceof
_判断对象是否是该构造函数构造出来的。_
```javascript
function Car() { };
//实例化Car
var car = new Car();
//声明Person对象
function Person() { };
//实例化person
var person = new Person();
console.log(car instanceof Car);//true
console.log(car instanceof Person);//false
console.log([] instanceof Object);//true;数组也是由Object构造出来的
console.log(car instanceof Object);//true;对象的原型链也包含了Object
console.log([] instanceof Array);//true;
console.log({} instanceof Object);//true;
A 对象的原型里到底有没有B的原型。
var a = [];
console.log(a.constructor);//数组的constructor是Array方法
console.log(a instanceof Array); //true a是Array的实例。
var str = Object.prototype.toSting.call(a);//
console.log(str)//[object,Array];
this
函数内部this
- 预编译函数this指向window;
- 全局函数this指向window
- apply/call改变this 指向
- 构造函数的this 被修改成了指向实例化对象
- 函数执行的时候 谁调用了这个函数这个函数就指向谁。
function Test() {
/**
* var this = {
* __proto__:Test.prototype
* }
*/
this.name = 123;
//return this;
}
var test = new Test();
/**
* AO {
* this: window->{
* __proto__:Test.prototype
* }
* }
*
* GO {
* this -> window
* test -> undefined
* Test ->function Test(){};
* }
*/
caller callee
例子1
function test(a, b, c) {
console.log(arguments.callee.length);
//形参个数
console.log(test.length);
//形参个数
console.log(arguments.length);
//返回实参个数
}
test(1,2,3)
例子2
```javascript //利用arguments.callee求0到n的和。 var sum = (function (n) { if (n <=1) {
} return n + arguments.callee(n - 1);//arguments.callee 指向函数本身。 })(10) //递归方法 function sum(n) { if (n <= 1) {return 1
} return n+sum(n-1) }return 1;
<a name="xp7OY"></a>
## 面试题:
<a name="IxRFC"></a>
### 题目1
在浏览器环境下。当apply 或者call 的时候第一个参数如果是undefined/null的时候、someFunction的this永远指向window;
```javascript
someFunction.apply(undefined/null,[])
function someFunction(){
console.log(this)//window
}
function foo() {
bar.apply(null, arguments);
bar.call(arguments);//等同于bar.apply(arguments)
}
function bar() {
console.log(arguments);
}
foo(1, 2, 3, 4, 5);
题目2
typeof 的返回值都有哪些
答:string/number/boolean/undefined/object(null,array)/function。
题目3
var f = (
function f() {
return 1
}
,
function g() {
return 2
}
)
// 此时f = function g(){ return 2};
typeof(f)// function
typeof(f())//f()执行返回2 所以是number
typeof((f()))//(2)转成了表达式 string
题目4
undefined == null;// null和undefined互相相当 true
undefined === null;//但是不全等。因为各自有各自的类型。false
isNaN('100');//字符串100会转成Number 所以是false
console.log(parseInt('1a')==1);// parseInt('1a')会 转成 1 所以为true
题目5
function isNaN1(num) {
var str = Number(num) + ''//加''是因为NaN不等于NaN;NaN不等于任何数所以NaN只能转成字符串。
if (str == 'NaN') {
return true;
} else {
return false;
}
}
题目6
{}=={}//不等于,因为引用值比较的是地址
//如何让他们相等 只能通过赋值、给地址
var a = {};
b =a;
a==b//true
题目7
var a = '1';
function test() {
var a = '2'; //AO{a:2}
this.a = '3';
console.log(a);
}
test();//2,执行这个的时候this 指向的window; this.a = 3;GO{a->3}
new test();//2,
console.log(a);//3
/**
* GO {
* a:undefined->'1',
* test:function(){....}
* }
* AO{
* a:undefined->'2',
* this:window
* }
*/
题目8
var a = 5;
function test() {
a = 0;// AO.a = 0;
console.log(a);// 0
console.log(this.a);test() this指向window.
var a;//变量提升
console.log(a);//0
}
test();//0,5,0
new test();//0,undefined,0;
/***
* 分析
* AO {
* a:undefined->0,
* this:window{a:5}
* }
* GO {
* a->undefined->5
* }
*/