<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/jquery-1.8.2.js"></script> <script> function abs(a){ console.log(a); console.log(arguments); for(var i=0;i<arguments.length;i++) { console.log("参数..."+arguments[i]); } } function lazy_sum(arr) { function sum() { return arr.reduce(function(x,y){ return x + y; }); } return sum; } function count() { var arr = []; for(var i=0;i<=3;i++) { arr.push((function(n) { return function (){ return n*n; } })(i)); } return arr; } function create_count(obj) { var x = obj || 0; return function(){ return x = x+1; } } </script> </head> <body> <script> $(function(){ /* * arguments学习 函数内部自带 专门接收并保存传入参数 */ //abs(10,20,30); /* * 理解call apply函数 保证this指向正确 一般为当前函数对象 * * call函数和apply函数 参数1:当前函数对象 若无,则传null * 参数2: call:按顺序传入函数对象的参数 * apply: 函数参数对象包装成数组 */ //abs.apply(null,[10,20,30]); //abs.call(null,10,30,50,60); /* * 创建闭包结构的函数 */ // var arr = [1,2,3,4,6]; // var f = lazy_sum(arr); // console.log(f); // console.log("运算结果:"+f()); /* * 创建一个匿名函数并立刻执行 */ //var arr = [1,2,3,4,6]; // var f = count(); // var f1 = f[2]; // console.log("返回函数:"+f1()); /* * 闭包理解 */ var f = create_count(); //var x = 0; // var f = { // inc:function(){ // x = x+1; // return x; // } // } // console.log(f); // console.log(f()); // console.log(f()); /* * 对象序列化 & json反序列化为Js对象 */ var obj = { name:"lizq", sex:"男", age:"27", job:function(){ console.log(this.name+" is a software engineer"); } } /* * 原型对象 */ var obj3 = { home:function(){ console.log(this.name + " 's home in HUBei") } } // console.log(obj); // console.log("序列化:"+JSON.stringify(obj,['name','job'],' ')) /* * 原型 */ var obj1 = { name:"chenyl" } //obj1对象原型指向obj //obj1.__proto__ = obj; //obj1对象原型指向obj3 // obj1.__proto__ = obj3; // obj1.home(); /* * 对象的原型 只能是 _proto_ 而构造函数的原型是 prototype * 构造函数原型 分两种: 1.对象的原型指向另一个对象 类似于Java继承 * 2.对象的原型指向构造函数 的原型对象 */ // function Student(name){ // this.name = name; // this.sayHello = function(){ // console.log(this.name+ " say hello"); // } // } // // var ins = new Student("chenyl"); //对象的原型指向Student 的原型 // console.log(ins.constructor === Student.prototype.constructor); // console.log(Student.prototype); // // /* // * 对于构造函数的原型链应该是 // * // * instance._proto_ --> Student.prototype(原型对象) --> object.proto_ --> null // * 而实例对象的原型则为原型对象 原型对象有一个属性为construstor 为该原型对象的构造函数 // */ /* * 原型深入学习 通过原型来写高层次代码 * hello函数其实通用 放到构造函数里意味着每次创建对象都要占用空间 * 用原型对象来放置这个通用函数 */ // Student.prototype.hello = function(){ // console.log(this.name+" 's home in HUBei"); // } // // var obj1 = new Student("lizq"); // var obj2 = new Student("cheyl"); // console.log(obj1); // console.log(obj2); /* * 按照约定,构造函数首字母应当大写,而普通函数首字母应当小写, * 这样,一些语法检查工具如jslint将可以帮你检测到漏写的new。 * 编写一个createStudent()函数,在内部封装所有的new操作 * 属性赋值放在构造函数里 函数声明放在原型对象上 * 根据对象属性查找原则,逐级向上查找 * 对象实例在读取某属性时,如果在本身的直属属性中没有查找到该属性, * 那么就会去查找function下的prototype的属性 */ // function Student(person) { // this.name = person.name || "未定义"; // this.grade = person.grade || "0"; // } // // Student.prototype.mthod = function() { // console.log(this.name + " got a goal is: "+this.grade); // } // // function createStudent(people) { // return new PrimaryStudent(people || {}); // } // // function PrimaryStudent(person) { // //调用student函数 // Student.call(this,person); // } // // createStudent({name:"chenyl",grade:"100"}).mthod(); // // function F() { // // } // F._initialized // console.log(F.prototype === new F()); /* * JavaScript的原型继承实现方式就是: * 定义新的构造函数,并在内部用call()调用希望“继承”的构造函数,并绑定this; * 借助中间函数F实现原型链继承,最好通过封装的inherits函数完成; * 继续在新的构造函数的原型上定义新方法。 * 总结:用构造函数方式定义对象的所有非函数属性,用原型方式定义对象的函数属性。 */ /*定义超类*/ function SuperClass(person) { this.name = person.name; console.log("已继承............"); this.age = person.age; /*父类函数*/ if(typeof SuperClass._inits == 'undefined') { SuperClass.prototype.superMthod = function (){ console.log("父类函数调用..........") } } SuperClass._inits = true; } /*定义子类*/ function SubClass(person) { /*调用父类函数*/ SuperClass.call(this,person); this.grade = person.grade; /*子类函数*/ if(typeof SubClass._inits == 'undefined') { SubClass.prototype.getGrade = function (){ return this.grade; } } SubClass._inits = true; } /* * 子类继承父类 --需要通过中间函数 * 子类复用父类的构造函数来初始化参数 */ // function extendImpl(subClass,superClass){ // function midWare(){} // midWare.prototype = superClass.prototype; // /*子类的原型对象指向中间函数实例*/ // subClass.prototype = new midWare(); // /*子类的构造函数指向自身*/ // subClass.prototype.constructor = subClass; // } // // /*定义实例化对象组件*/ // function createPerson(person) { // return new SubClass(person || {}); // } // // var obj1 = new createPerson({name:"lizq",age:"27",grade:"35"}); // console.log(obj1.getGrade()); var someuser = { name:'byvoid', func:function() { console.log(this.name); } }; var foo = { name:'foobar' }; foo.func = someuser.func; foo.func(); //输出foobar foo.func1 = someuser.func.bind(someuser); foo.func1(); //输出byvoid func = someuser.func.bind(foo); func(); //输出foobar func2 = func; func2(); //输出foobar }); </script>
</body>
</html>