JavaScript中的匿名函数与继承

匿名函数

匿名函数的优势
1.可以节省全局变量
2.可以节省一些代码:讲一个函数调用操作委托给另一个函数
3.有助于提高性能
匿名函数笔记
原贴:js匿名函数和闭包总结 - 范仁义 - 博客园 (cnblogs.com)
一句话总结:匿名函数的最主要作用是创建闭包,闭包就是将函数内部和函数外部连接起来的一座桥梁.内层的函数可以使用外侧函数的所有变量,即使外层函数已经执行完毕. 闭包可以用来模仿块级作用域等等.
匿名函数基本格式:

var fn = function(){}//匿名函数赋值给变量

(function(){//匿名函数通过表达式自我执行
)();

var name = 'The window';
var obj={
  name:'my obj';
  get:function(){
    return function(){
      return this.name;
    }
  }
}
obj.get()();//全局变量name的值'The window'
obj.get().call(obj);//使用call()强制this指向obj

闭包的作用
1.通过闭包可以访问局部变量
2.可以让局部变量始终保持在内存中:避免使用全局变量
3.循环函数中的匿名函数和闭包问题

function add(){
    var num = 100;
    return function(){num++;}
};
var fn = add();//初始化
fn();//执行里面的匿名函数
fn=null;//记得要释放内存

getter和setter

var getValue,setValue;
(function(){
    var secret = 0;
    getValue = function(){
        return secret;
    };
    setValue = function(v){
        secret = v;
    };
})

迭代器

function setup(x)
{
    var i=0;
    return function(){
        return x[i++];
    }
}
var next = setup([1,2,3]);

对象

function f(){return arguments.callee;};//返回自身引用

//匿名函数递归调用
(
    function(count)
    {
        if(count < 5)
            arguments.callee(++count);
    }
)(1);

所有的引用类型都继承了 Object,所有的对象都拥有 Object 具有的一些默认的方法。如:hasOwnProperty()、propertyIsEnumerable()、toLocaleString()、toString() 和 valueOf()

继承

简单的继承

function A(){
    this.name = "A";
};
function B(){
    this.name = "B";
}
A.prototype = new B();
//对prototype完全重写时,会对它的constructor产生负面影响
A.prototype.constructor = A;

JavaScript中的8种继承
原贴:https://cloud.tencent.com/developer/article/1851143

1.原型链继承
缺点:
无法实现多继承
引用类型的值会被实例共享
子类型无法给超类型传递参数

function Parent(){
    this.parentName = "father";
}
Parent.prototype.getParentName = function(){
    return this.parentName;
}
function Child(){
    this.childName = '子类';
}
Child.prototype = new Parent();
Child.prototype.getChildName = function(){
    return this.childName;
}
console.log(new Child().getParentName());

2.借用构造函数继承(对象冒充)
缺点:
不能继承超类原型上的属性和方法
无法实现函数复用,由于 call 有多个父类实例的副本,性能损耗
原型链丢失

function Parent(name){
    this.name = name;
}
function Child(name){
    Parent.call(this,name);//将Parent的name属性复制到Child中
    this.age = 24;
}
var c1=new Child("ming");
console.log(c1.name);

3.组合模式继承(原型链+借用构造函数继承)
缺点:
调用了 2 次超类型构造函数:一次在子类构造函数内,另一次是将子类的原型指向父类构造的实例

function Parent(name) {
    this.name = name;
}
Parent.prototype.getName=function()
{
    return this.name;
}
function Child(name)
{
    Parent.call(this,name);
    this.age = 24
}
//补充[借用构造函数继承]中丢失的原型链
Child.prototype = new Parent('父类');
var c1 = new Child("c1");
console.log(c1.getName()==c1.name);//true

4.共享原型继承
缺点:
只能继承父类原型属性方法,不能继承构造函数属性方法
引用类型的值会被实例共享

function Parent(){}
Parent.prototype.hobbies = ["sing","dance","rap"];
function Child(name)
{
    this.name = name;
}
Child.prototype = Parent.prototype;
var c1 = new Child('c1');
console.log(c1.hobbies);

5.原型式继承:ES5 新增了 Object.create() 方法规范化了原型式继承

function createAnother(o)
{
    function F(){}
    F.prototype = o;
    return new F();
}
var o1 = {
    name:'父类对象',
    say:function(){}
}
var o2 = createAnother(o1);
console.log(o2.name);

6.寄生式继承:结合原型式继承和工厂模式,创建一个仅用于封装继承过程的函数

function createAnother(origin)
{
    var clone = Object.create(origin);//原型式继承
    clone.sayHi = function(){console.log("Hi");}
    return clone;
}
var o1 = {
    name:'父对象',
    hobbies:["sing","dance","rap"]
};
var o2 = createAnother(o1);
o2.sayHi();

7.寄生组合式继承:JavaScript中最完美的继承方式

//写法
function inheritPrototype(SubType,SuperType)
{
    var prototype = Object.create(SuperType.prototype);
    prototype.constructor = SubType;
    SubType.prototype = prototype;
}
//实例
function Parent(name)
{
    this.name = name;
}
Parent.prototype.getName = function(){
    return this.name;
}
function Child(name){
    Parent.call(this,name);
    this.age = 24;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

var c1 = new Child('c1');
console.log(c1.name);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容