// JavaScript对象编程
// 1. 对象(OOP)是什么
// (1)对象是单个实物的抽象
// (2)对象是一个容器,封装了属性(property)和方法(method)。
// 2. 构造函数
// javaScript的对象体系是基于构造函数(construction)和原型链(prototype)
// 构造函数特点:
// (1)函数体内部使用了this关键字,代表了所要生成的对象实例。
// (2)生成对象的时候,必须使用new命令。
// (3)构造函数名字的第一个字母通常是大写的。
// var Vehicle = function () { this.price = 100 }
// 3. new 命令
// 3.1 基础用法
// new命令的作用:执行构造函数,返回一个实例。
// var v = new Vehicle(); --> v.price //==>100
// new命令执行时,构造函数内部的this,代表了新生成的实例对象。
// 构造函数可以接收参数
// var Vehicle = function(p){this.price = p}; var v = new Vehicle(500);
// 如果忘了使用new命令,构造函数就变成了普通函数,并不会生成实例对象。this就代表全局对象。
// (1)构造函数内部使用严格模式,保证构造函数必须与new命令一起使用。
// 在构造函数内第一行加上 'use strict';
// (2)在构造函数内部判断是否使用new命令。
/* function Fubar(foo,bar){
if(!(this instanceof Fubar)){
return new Fubar(foo,bar)
}
this._foo = foo;
thia._bar = bar;
}
Fubar(1,2)._foo ==> 1
(new Fubar(1,2))._foo ==> 1
*/
// 3.2 new 命令的原理
// new命令的执行过程:
// (1)创建一个空对象,作为将要返回的对象实例。
// (2)将这个空对象的原型,执行构造函数的prototype属性。
// (3)将这个空对象赋值给函数内部的this关键字。
// (4)开始执行构造函数内部的代码。
// 构造函数内部,this值的时一个新生的空对象,所有针对this的操作都会发生在这个空对象上。
// 如果构造函数内有return一个对象,那么new会返回return指定的对象(必须是对象,不是对象还是返回this),否则返回this
// 如果对普通函数使用new命令,则会返回一个空对象。
// new命令的函数
/* function _new(constructor,params){
// 将arguments对象转为数组
var args = [].slice.call(arguments)
// 取出构造函数
var constructor = args.shift()
// 创建一个空对象,继承构造函数的protptype属性
var context = Object.create(constructor.prototype)
// 执行构造函数
var sult = constructor.apply(context,args);
return (typeof result == 'object' && result != null) ? result : context
}
实例:var actor = _new(Person,'zhangsan',20)
*/
// 3.3 new.target
// 函数内部可以使用new.target属性,如果当前函数是new命令调用,new.target指向当前函数,否则为undefined。
// function f(){if(!new.target){throw new Error("请使用new命令调用!")}}
// 4. Object.create()创建实例对象
// 构造函数作为模板,可以生成实例对象。
// 当拿不到构造函数,只能拿到一个现有的对象时,使用create()方法以这个现有对象为模板生成一个新的实例对象。
// var person1 = {name:"zhangsah",age:12,greating:function(){console.log("hello"+this.name)}}
// var person2 = Object.create(person1)
// person2.name ==>zhangsan
// person2.greeting() ==> hellozhangsan
// 对象person1是模板,person继承了前者的属性和方法
//
//
// 5. this关键字
// this总是返回一个对象。this就是属性和方法‘当前’的对象。
// this.property this就代表property当前所在的对象。
// this的指向是可变的。
// var A = {
// name:"张三",
// describe:function(){
// return "姓名"+this.name;
// }
// }
// var name = "李四"
// var f = A.describe;
// f() ==> "姓名:李四"
// A.describe被赋值给变量f,内部的this会指向f运行时所在的对象(顶层对象)
// 6. this的使用场景
// 6.1 全局环境
// 全局环境使用this,指顶层对象window
// 不管是不是再函数内部,只要在全局环境下运行,this就是顶层对象window
// 6.2 构造函数
// 构造函数中的this指的是实例对象。
//
// 6.3 对象的方法中
// 指向方法运行时所在的对象,该方法赋值给另一个对象会改变this的指向
// 如果this所在的方法不在对象的第一层,这是this只是指向当前一层的对象,而不会向上继承。
// 将对象内部的方法赋值给一个变量,this会指向全局变量。将对象赋值,this指向不会变。
// 7. this使用注意点
// 避免多次this
// 避免数组处理方法中的this
// 避免回调函数中的this,回调函数中的this往往会改变指向
// 8. 绑定this的方法
// js提供call,apply,bind三种方法来切换/固定this的指向
// 8.1 Function.prototype.call()
// call()方法的参数为一个对象,this为参数对象的this。如果参数为空,null或者undefined,则迷人传入全局对象。
// 如果call()参数是一个原始值,那么这个原始值会自动转为对应的包装对象,然后传入call方法。
// var f = function(){return this;}; f.call(6); => Number{[[PrimitiveValue]]:6}
// call()方法可以接收多个参数。
// func.call(thisValue,arg1,arg2....)
// 第一个参数是this要指向的那个对象,后面的参数是函数时所需要的参数
// 8.2 Function.prototype.apply()
// apply和call()类似,只不过传入参数形式为数组。
// apply(thisValue,[arg1,arg2,arg3...])
// 转换类数组的对象,将一个类数组转为真数组
// Array.prototype.slice.apply({0,1},length:1) ==> [1]
// 8.3 Function.prototype.bind()
// bind()方法用于将函数体内的this绑定到某个对象,然后返回一个新函数。
// var d = new Date();
// d.getTime() //14846686....
// var print = d.getTime;
// print() // ==> this.is not a Date object;
// // 赋值后this不指向Data对象实例了
// var print = d.getTime.bind(d);
// print() ==> //14876464845...
// bind()方法将getTime内部的this绑定到d对象
// bind方法的参数就是索要绑定this的对象
// var counter = {
// count:0,
// inc:function(){this.count++;}
// };
// var func = counter.inc.bind(counter);
// func();
// counter.count //==> 1
// counter.inc 方法被赋值给func,bind将inc内部的this绑定到counter上
// bind()可以接收更多的参数,将这些参数绑定到原函数参数。
// var add = function(x,y){ return x * this.m + y * this.n }
// var obj = {m:2,y:3}
// var newAdd = add.bind(obj,5)
// newAdd(4) ==> 5*2+3*4
// bind除了绑定this,还将add函数的第一个参数绑定成5,然后返回newAdd函数,这个函数只需要再接收一个参数y就可以了
// 如果bind第一个参数时null或者undefined,等于将this绑定到全局对象,函数运行时指向顶层对象。
// bind()方法注意:
// (1)bind方法每执行一次,会返回 一个新函数。监听事件时不能这样使用。
// Element.addEventListener('click',o.m.bind(o))
// click事件绑定bind方法生成一个匿名函数,这样会导致无法取消绑定。所有不能使用removeElementListener()取消
// 正确的使用方式:
// var listener = o.m.bind(o)
// element.addEventListener('click',listener)
// .....
// element.removeElementListener('click',listener)
// (2). 结合回调函数使用
// var counter = {
// count:0,
// inc:function(){
// 'use strict';
// this.count ++;
// }
// }
// function callIt(callback){
// callback();
// }
// callIt(count.inc.bind(counter))
// counter.count // ==> 1
// // 使用bind将counter.inc 绑定到counter上,this总是指向counter
// (3)结合call方法使用
springboot中为何参数拿对象接收总是报错_JavaScript对象编程构造函数和this详解。...
本文深入探讨JavaScript中的对象编程,包括对象的概念、构造函数的特性和用法,new命令的工作原理,以及this关键字在不同场景下的指向。通过实例解析构造函数中的this、new.target属性,以及如何通过call、apply、bind方法绑定或切换this的指向。同时,介绍了Object.create()方法创建实例对象,并讨论了this在全局环境、构造函数和对象方法中的应用。
摘要由CSDN通过智能技术生成