版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/oncemore520/article/details/80742571
函数可谓是js中的重点部分,这里我没有画思维导图,直接搜索了一些关于闭包和继承这类的一些知识点
//闭包
//函数外部和函数内部沟通的桥梁
//闭包的几种写法
function Circle(r){
this.r = r;
}
Circle.PI =3.14;
Circle.prototype.area = function(){
return Circle.PI * this.r *this.r
}
var c = Circle(1);
alert((c.area()));//只是给函数添加方法
//2.*
var Circle = function(){
var obj = new Object();
obj.PI = 3.14;
obj.area = function(r){
return this.PI *r *r
}
return obj;
}
var c = new Circle();
alert(c.area(1));
//3
var Circle = {
"PI":3.14,
"area":function(r){
return this.PI * r *r
}
}
//闭包的用途
//1.匿名自执行函数 UI的初始化,有的函数只执行一次,内部变量无需维护,
var data= {
table : [],
tree : {}
};
(function(dm){
for(var i = 0; i < dm.table.rows; i++){
var row = dm.table.rows[i];
for(var j = 0; j < row.cells; i++){
drawCell(i, j);
}
}
})(data);
//2.结果缓存
var CachedSearchBox = (function(){
var cache = {},
count = [];
return {
attachSearchBox : function(dsid){
if(dsid in cache){//如果结果在缓存中
return cache[dsid];//直接返回缓存中的对象
}
var fsb = new uikit.webctrl.SearchBox(dsid);//新建
cache[dsid] = fsb;//更新缓存
if(count.length > 100){//保正缓存的大小<=100
delete cache[count.shift()];
}
return fsb;
},
clearSearchBox : function(dsid){
if(dsid in cache){
cache[dsid].clearSelection();
}
}
};
})();
CachedSearchBox.attachSearchBox("input");
//3.封装
var person = function(){
//变量作用域为函数内部,外部无法访问
var name = "default";
return {
getName : function(){
return name;
},
setName : function(newName){
name = newName;
}
}
}();
print(person.name);//直接访问,结果为undefined
print(person.getName());
person.setName("abruzzi");
print(person.getName());
// 得到结果如下:
//
// undefined
// default
// abruzzi
//实现类和继承
function Person(){
var name = "default";
return {
getName : function(){
return name;
},
setName : function(newName){
name = newName;
}
}
};
var p = new Person();
p.setName("Tom");
alert(p.getName());
var Jack = function(){};
//继承自Person
Jack.prototype = new Person();
//添加私有方法
Jack.prototype.Say = function(){
alert("Hello,my name is Jack");
};
var j = new Jack();
j.setName("Jack");
j.Say();
alert(j.getName());
继承的一些知识:
https://blog.csdn.net/qq_17335153/article/details/52594134