1.给指定对象新增一些属性的写法:

 /*给一个对象增加属性*/
var myDate=(function(obj){
obj.addName=function(name){
this.name=name;
}
return obj;
}(myDate||{}));
/*此用myDate||{}这种方式确保传进去的是一个对象*/

2.重载指定对象的某个方法:

     var myDate2={name:'zhangsan',
addAge:function(age){
this.age=age;
}
}; var myDate2=(function(obj){
var oldAddAge=obj.addAge;//保留以前的方法,以后可以调用
obj.addAge=function(age){
oldAddAge(age+1);//这样写能重载myDate2的addAge方法吗?当然不能,这个age被加到了window对象上了。
}
return obj
}(myDate2||{})); myDate2.addAge(10);

上面的写法是不行的,这样不能实现重载对象的某个方法,原因是对象原来的addAge方法中this指向了window,无法绑定到myDate2上面。

     var myDate2={name:'zhangsan',
addAge:function(age){
this.age=age;
}
}; var myDate2=(function(obj){
//var oldAddAge=obj.addAge;
/*
//这个地方不能再保留以前的函数供以后使用了,原因是addAge函数中使用了this,如果再次引用
//this就指向了window对象。故重载函数中不能再调用原来的函数。
*/
obj.addAge=function(age){
this.age=age+10;
}
return obj;
}(myDate2||{})); myDate2.addAge(10);

换成这种方式,可以重载对象中的addAge方法。

3.使用原型做一个简单加减运算:

     var calc=function(numx,operx){
this.numx=numx;
this.operx=operx;
}
calc.prototype={
add:function(a,b){
return a+b;
},
sub:function(a,b){
return a-b;
}
}; var c=(new calc).add(3,4);

4.函数,对象,constructor,prototype之间的关系:

 var FOO=function(){
this.name='zhangsan';
}
FOO.prototype.getName=function(){
return this.name;
} var p=new FOO();
var d={};
console.log(p.constructor=== FOO);// true,用函数构造的对象,其构造函数是该函数本身
console.log(d.constructor===Function);//flase
console.log(d.constructor===Object);//true,直接生成的对象,其构造函数是Object
console.log(FOO.constructor===Function);//true,函数本身的构造函数,是Function
console.log(p.constructor.constructor===Function);//true,这个是一个传递
console.log(FOO.prototype.constructor===FOO);//true,每个函数都有一个prototype,其prototype.constructor指向该函数本身

当重写函数的prototype后,由函数够造的对象的构造函数会发生变化:

         var BOO=function(){
this.name='zhangsan';
}
BOO.prototype={age:function(age){
this.age=age;
}
};//注意与上面的那种写法不一样,这种是重写,上面那种只是修改
var b=new BOO();
console.log(b.constructor===BOO);//false,由于重写了函数BOO的prototype导致constructor变化
console.log(BOO.constructor===Function);//true
console.log(BOO.prototype.constructor===BOO);//false,重写了prototype导致constructor变化
         var BOO=function(){
this.name='zhangsan';
}
BOO.prototype={age:function(age){
this.age=age;
}
};
/*
BOO.prototype=new Object({age:function(age){this.age=age;}});
上面重写prototype相当于这种方法
*/
var b=new BOO();
console.log(b.constructor===Object);//true
console.log(BOO.prototype.constructor===Object);//true

如何修改BOO的构造函数呢?

         var BOO=function(){
this.name='zhangsan';
}
BOO.prototype={age:function(age){
this.age=age;
}
};
BOO.prototype.constructor=BOO;//重写BOO函数的prototype.constructor以后可以修复这个问题
var b=new BOO();
console.log(b.constructor===BOO);//true
console.log(BOO.prototype.constructor===BOO);//true

javascript匿名函数应用的更多相关文章

  1. (转)Javascript匿名函数的写法、传参、递归

    (原)http://www.veryhuo.com/a/view/37529.html (转)javascript匿名函数的写法.传参和递归 javascript匿名函数的写法.传参和递归 http: ...

  2. (转)javascript匿名函数的写法、传参和递归

    (原)http://www.veryhuo.com/a/view/37529.html (转)javascript匿名函数的写法.传参和递归 http://www.veryhuo.com 2011-0 ...

  3. 第一百一十节,JavaScript匿名函数和闭包

    JavaScript匿名函数和闭包 学习要点: 1.匿名函数 2.闭包 匿名函数就是没有名字的函数,闭包是可访问一个函数作用域里变量的函数.声明:本节内容需要有面向对象和少量设计模式基础,否则无法听懂 ...

  4. JavaScript匿名函数的使用

    JavaScript匿名函数的使用:  http://www.cnblogs.com/skykang/archive/2010/12/03/1895274.html 一.什么是匿名函数? 在Javas ...

  5. javascript 匿名函数的理解(转)

    原网址 http://www.jb51.net/article/21948.htm javascript 匿名函数的理解(透彻版) 代码如下: (function(){ //这里忽略jQuery所有实 ...

  6. 简单介绍Javascript匿名函数和面向对象编程

    忙里偷闲,简单介绍一下Javascript中匿名函数和闭包函数以及面向对象编程.首先简单介绍一下Javascript中的密名函数. 在Javascript中函数有以下3中定义方式: 1.最常用的定义方 ...

  7. [从jQuery看JavaScript]-匿名函数与闭包(Anonymous Function and Closure)【转】

    (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的第一眼,我就迷糊了.为什么只有 ...

  8. javascript 匿名函数的理解,js括号中括function 如(function(){})

    代码如下: (function(){  //这里忽略jQuery所有实现  })();  (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也 ...

  9. 浅谈JavaScript匿名函数与闭包

    一. 匿名函数   //普通函数定义: //单独的匿名函数是无法运行的.就算运行了,也无法调用,因为没有名称. 如: function(){             alert('123');    ...

  10. 深入理解javascript 匿名函数和闭包

    代码如下: (function(){ //这里忽略jQuery所有实现 })(); (function(){ //这里忽略jQuery所有实现 })();  半年前初次接触jQuery的时候,我也像其 ...

随机推荐

  1. eclipse控制台中文乱码解决方法

    一.全局设置 1.Window > Preferences 2.General > Workspace > Text file encoding. 3.选择 Other 4.手工输入 ...

  2. Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  3. 彩票号码OC呈现

    +(NSArray*)splitCode:(NSString*)code backzoneIndex:(NSInteger*)outIndex { /* 01 0102|0304 0102030405 ...

  4. iOS开发 适配iOS10以及Xcode8

    iOS10的适配以及Xcode8使用上的一些注意点 一.证书管理 用Xcode8打开工程后,比较明显的就是下图了,这个是苹果的新特性,可以帮助我们自动管理证书.建议大家勾选这个Automaticall ...

  5. iOS 汉字拼音

    获取汉字的拼音 #import <Foundation/Foundation.h> @interface NSString (Utils) /** *  汉字的拼音 * *  @retur ...

  6. SecWeek

    “叮铃铃,叮铃铃”清晨闹钟如期而至,每天的这个时候都会是一天中最头痛的时刻,每日坚持早起,渐渐已经开始习惯,扶着沉重的额头,侧身翻起,会觉得世界都在天旋地转. 一个人,悄悄的开门,悄悄的刷牙洗脸,然后 ...

  7. 请求网络get

    package com.baidu.net; import java.io.IOException; import org.apache.http.HttpEntity;import org.apac ...

  8. MyEclipse黑色主题

    第一步:打开链接http://www.eclipsecolorthemes.org/选中一款:下载其中的epf格式. 如图: 在eclipse中打开:file > import > Gen ...

  9. HDU-2825 Wireless Password(AC自动机+状压DP)

    题目大意:给一系列字符串,用小写字母构造出长度为n的至少包含k个字符串的字符串,求能构造出的个数. 题目分析:在AC自动机上走n步,至少经过k个单词节点,求有多少种走法. 代码如下: # includ ...

  10. Google Java编程风格指南

    出处:http://hawstein.com/posts/google-java-style.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Comm ...