js高级程序设计(第三版) 兼容bug
数组(Array)
构造函数创建数组
- new Array(arg)
arg为数字,则创建一个对应数量的数组
arg为字符串,则创建包含arg个的一个数组
new操作符可以省略
对象字面量创建数组
- bug
arr 1
2
3
4
在ie中arr的值为1,2,undefined,而在其它浏览器中arr的值为1,2
2. ```let arr = [,,,,]
在ie8以下
1 |
|
若改变arr的length长度,则会改变数组对应的数据
1 |
|
可以借助sort方法的参数(比较函数)得到正确的结果
1 | let arr = [0,1,5,10,15]; |
对象(Object)
对象属性类型
- 数据属性 可以直接定义
数据属性包含一个数据的位置,在这个位置可以读取和写入,数据属性有4个描述其属性的行为特性
- [[ Configureable ]] 表示能否通过delete直接删除其属性,默认值true
- [[ Enumerable ]] 表示该属性能否通过
... in ...```进行遍历,默认值为true 1
2
3
4
5
6
* * [[ Writable ]] 表示该属性的值能否修改,默认值为true
* * [[ Value ]] 表示该属性的数据值,默认值为undefined
<font color="red">以上为每个对象的属性都具有的特性,要想修改其默认属性,必须同过```Object.defineProperty()```方法</font>
- [[ Enumerable ]] 表示该属性能否通过
Object.defineProperty(属性所在的对象,属性的名字,描述符)
//描述符为以上四个属性,这样就可以修改默认值了
//例子
var person = {}
Object.defineProperty(person,”name”,{
value:”caicai”,
writable:false
})
console.log(person.name) // caicai
person.name=”hahaha”; //严格模式下报错,非严格模式下将会忽略
console.log(person.name) // caicai
1 |
|
let book={
year:2004,
edition:1
}
Object.defineProperty(book,”year”,{
get:function(){
return this.year;
},
set:function(newValue){
if(newValue>this.year){
this.edition += newValue - this.year;
this.__year = newValue;
}
}
})
book.year = 2015;
console.log(book.edition)//12
1 |
|
//工厂模式
funcction CreatePerson(name,age,job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName=function(){
return this.name
}
return o;
}
var p1 = CreatePerson(“caicai1”,20,”民工”)
var p2 = CreatePerson(“caicai2”,22,”IT民工”)
1 |
|
//方式一
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
return this.name
}
}
var p1 = new Person(“caicai1”,20,”民工”)
var p2 = new Person(“caicai2”,22,”IT民工”)
//方式二
function sayName(){
return this.name
}
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = sayName;
}
var p1 = new Person(“caicai1”,20,”民工”)
var p2 = new Person(“caicai2”,22,”IT民工”)
1 |
|
function Person(){}
Person.prototype.name= “caicai”
Person.prototype.age= 20
Person.prototype.job= “It”
Person.prototype.sayName= function(){
return this.name
}
var p1 = new Person();
p1.sayName()//caicai
p1.hasOwnPropertype(“name”)//false
p1.name = “hahha”
p1.hasOwnPropertype(“name”)//true
delete p1.name
p1.hasOwnPropertype(“name”)//false
var p2 = new Person();
p2.sayName()//caicai
1 |
|
function Parent(){}
var p1 = new Parent();
Parent.prototype.constructor === Parent //true
p1.proto === Parent.prototype //true
1 |
|
function hasPrototypeProperty(object,name){
return !object.hasOwnProperty(name)&&(name in object);
}
function Person(){}
var p = new Person();
Person.prototype.isPrototypeOf(p) //true
1 |
|
function Person(name,age){
this.name = name;
this.age = age;
}
Person.prototype = {
constructor:Person,
sayName:function(){
return this.name
}
}
1 |
|
function Person(name,age){
this.name = name;
this.age = age;
if(typeof this.sayName!=undefined){
Person.prototype.sayName = function(){
return this.name
}
}
}
1 |
|
* 借用构造函数
在子类型的内部调用父类型的构造函数
1 | function Person() { |
* 组合继承
将借用构造函数继承和原型链继承结合在一起
1 | function Person( name ) { |