js基于对象
为什么是基于对象,不是面向对象,因为面向对象有:封装,继承,多态。js只有封装 js函数没有重载。多个方法名相同,只执行最后一个函数,最后一个会覆盖前面的函数
什么是重载:函数名相同,参数不同(参数个数不同,参数 顺序不同 ,参数类型不同)
function test ( ) {
alert ( 'test' ) ;
}
function test ( x) {
alert ( "x=" + x) ;
}
function test ( x, y) {
alert ( "x=" + x + ",y=" + y) ;
}
test ( 10 ) ;
js函数的形参是占位符,没有实际意义,如果不用形参,也可以用arguments[i]取出
function test ( ) {
alert ( arguments[ 0 ] ) ;
alert ( arguments[ 1 ] ) ;
}
test ( 10 , 20 ) ;
js创建对象的三种方式:
自定义一个类(实质是函数),再创建对象(js的静态方法需要在类外面创建) //定义类(定义了一个类叫做Student、并且为这个类同时定义了一个构造函数Study)
function Student ( sno, name, age) {
this . sno = sno;
this . name = name;
this . age = age;
this . study = function ( ) {
alert ( '我是' + this . name + ",我在学习JS" ) ;
}
}
let stu = new Student ( 1 , 'LX' , 22 ) ;
stu. study ( ) ;
let student = new Object ( ) ;
student. sno = 1 ;
student. name = 'LX' ;
student. age = 22 ;
student. study = function ( ) {
alert ( '我是' + this . name + ",我在学习JS" ) ;
}
student. study ( ) ;
delete student. name;
student. study ( ) ;
let student = {
sno: 1 ,
name: 'LX' ,
age: 22 ,
study: function ( ) {
alert ( '我是' + this . name + ',我在学习JSON' ) ;
} ,
} ;
let stu = new student ( ) ;
stu. study ( ) ;
let objArr = [ {
sno: 1 ,
name: 'LX' ,
age: 22 ,
study: function ( ) {
alert ( '我是' + this . name + ',我在学习JSON' ) ;
} ,
} , {
sno: 2 ,
name: 'LXX' ,
age: 21 ,
study: function ( ) {
alert ( '我是' + this . name + ',我在学习JSON' ) ;
} ,
} , {
sno: 3 ,
name: 'LXXXX' ,
age: 23 ,
study: function ( ) {
alert ( '我是' + this . name + ',我在学习JSON' ) ;
} ,
} ]
for ( let i= 0 ; i< objArr. length; i++ ) {
objArr[ i] . study ( ) ;
}
减少全局变量的污染,两种方法
在不同的js文件中创建一个相同的变量,定义成唯一的属性,并导入
var myWeiXin = { } ;
myWeiXin. table = "桌子A" ;
B . js
var myQQ = { } ;
myAppQQ. table = "表格B" ;
function outer ( ) {
let a = 10 ;
function inner ( ) {
alert ( "a=" + a++ ) ;
}
return inner;
}
let obj = outer ( ) ;
obj ( ) ;
obj ( ) ;
function outer2 ( ) {
let a = 30 ;
function inner2 ( ) {
alert ( "a=" + a++ ) ;
}
return inner2;
}
let obj2 = outer2 ( ) ;
obj2 ( ) ;
obj2 ( ) ;
闭包作用的概括:函数外可以访问函数内的局部变量 闭包的好处: (1)希望—个变量长期驻扎在内存当中(不被垃圾回收机制回收) (2)避免全局变量的污染 (3私有成员的存在 (4)安全性提高
模拟继承,三种方式
function Person ( name, age) {
this . name = name;
this . age = age;
this . eat = function ( ) {
alert ( '我是' + this . name + ",我在吃饭" ) ;
}
}
function Student ( sno, name, age) {
Person. call ( this , name, age) ;
this . sno = sno;
this . study = function ( ) {
alert ( '我的学号是:' + this . sno + ',我在学习模拟继承' ) ;
}
}
let stu = new Student ( 1 , 'LX' , 22 ) ;
stu. eat ( ) ;
function Person ( name, age) {
this . name = name;
this . age = age;
this . eat = function ( ) {
alert ( '我是' + this . name + ",我在吃饭" ) ;
}
}
function Student ( sno, name, age) {
Person. apply ( this , [ name, age] ) ;
this . sno = sno;
this . study = function ( ) {
alert ( '我的学号是:' + this . sno + ',我在学习模拟继承' ) ;
}
}
let stu = new Student ( 1 , 'LX' , 22 ) ;
stu. eat ( ) ;
function Person ( name, age) {
this . name = name;
this . age = age;
this . eat = function ( ) {
alert ( '我是' + this . name + ",我在吃饭" ) ;
}
}
function Student ( sno, name, age) {
Person. call ( this , name, age) ;
this . sno = sno;
this . study = function ( ) {
alert ( '我的学号是:' + this . sno + ',我在学习模拟继承' ) ;
}
}
let person = new Person ( ) ;
Student. prototype = person;
let stu = new Student ( 1 , 'LX' , 22 ) ;
function UnivercityStudent ( ) {
}
let stu2 = new Student ( ) ;
UnivercityStudent. prototype = stu2;
let uniStudent = new UnivercityStudent ( ) ;
console. log ( Person. prototype) ;