function Monkey(_type, _home){ this.type = _type; this.home = _home; this.say = function(){ console.log("我是一只猴子住在"+ this.home); } } function Magic_monkey(_type, _home,arr){ Monkey.call(this, _type, _home); this.skill = arr; } var wukong = new Magic_monkey("猴子", "花果山", ["七十二变","筋斗云"]); console.log(wukong.type + wukong.home + wukong.skill) wukong.say() function aaa(name, home){ this.name = name; this.home = home; } aaa.prototype.say = function(){ console.log("我是一只小猴子,我家住在:"+this.home); } function bbb(_name, _home, _skill){ aaa.call(this, _name,_home); this.skill = _skill; } bbb.prototype.say =function(){ console.log("我的技能为:" +this.skill); } var pig = new bbb("猪", "圈", ["吃", "睡觉"]); pig.say() function Hero(type, home, weapon){ this.type = type; this.home = home; this.weapon = weapon? weapon: "木有武器"; this.skill = function(){ console.log(this.type + "向敌人发起攻击") } } var user = new Hero("小站", "南方", "到"); console.log(user.type + user.home + user.weapon) delete user.type; delete user.home; user.sex = '男'; for (var i in user){ if(typeof(user[i]) == 'function'){ console.log('方法-'+ i + ':' + user[i]); } else{ console.log('属性-' + i +":" + user[i]) } } function fn(){ return function(){ return "我是匿名函数" } } console.log(fn); console.log(fn()); console.log(fn()()) function a(){ var txt = "局部变量x"; return function(){ return txt; } } console.log(a()()) function add(){ var num = 1; return function(){ num++; console.log(num); } } var c = add(); c() c() c() function class1(){ var arr = []; for(var i =0;i<5; i++){ arr[i] = "元素:"+i; } return arr; } console.log(class1()) function class2(){ var arr1 = []; for(var i =0; i<5; i++){ arr1[i] = (function(){ return '元素' + i })() } return arr1 } console.log("arr1:" + class2()); function class3(){ var arr2 = []; for(var i = 0;i<5;i++){ arr2[i] = function(n){ return function(){ return '元素'+n; } /*return '元素' + n*/ }(i) } return arr2 } var bb = class3(); for(var i =0; i<5; i++){ console.log(bb[i]()) } var obj = { name: '111', get:function(){ return this.name } } console.log("this:" + obj.get()) var title = "测试this"; var newobj = { title: "111", get:function(){ var _this = this; return function(){ return _this.title } } } console.log("this:" + newobj.get()());