Closure-access to basic data types and reference types

We often use closure methods to access variables inside the function from outside the function

Access a basic variable type

Protect basic data variables so that they cannot be directly accessed (read, write)

function operateBaseVariable(){
    
    
	let a = 10
	function getA() {
    
    
		return a
	}
	function setA(any){
    
    
		a = any
	}
	return {
    
    
		getA,
		setA,
	}
}
let opertaeBase = operateBaseVariable()//这是一个对象,对象里面包含两个函数
//获取a的值
opertaeBase.getA()
//设置a的值
opertaeBase.setA('a 被重新设置了')

This is an encapsulation of sensitive data a. One of the principles of encapsulation is to expose as little internal implementation methods as possible. Although data a is protected here, we have exposed the method of operating it, so we can improve:

function operateBaseVariable(){
    
    
	let a = 10
	function getA() {
    
    
		return a
	}
	function setA(any){
    
    
		a = any
	}
	return {
    
    
		getBaseA: getA,
		setBaseA: setA,
	}
}
let opertaeBase = operateBaseVariable()//这是一个对象,对象里面包含两个函数
//获取a的值
opertaeBase.getBaseA()
//设置a的值
opertaeBase.setBaseA('a 被重新设置了')

Here, if we want to operate and access data a, we can only use getBaseA() and setBaseA(). Then other developers don't know how to read and write a internally, only know that they can be done through these two methods. Operation

Access a reference data type (array)

How to protect an array? Go directly to the code

function operateComplexVariable(){
    
    
	let arr = [1,2,3]
	function getArr() {
    
    
		let newArr = [...arr] //深拷贝
		return newArr
	}
	function pushArr(any){
    
    
		arr.push(any)
	}
	return {
    
    
		getComplexArr: getArr,
		pushComplexArr: pushArr,
	}
}
let opertaeComplex = operateComplexVariable()//这是一个对象,对象里面包含两个函数
//获取arr的值
opertaeBase.getComplexArr()
//设置a的值
opertaeBase.pushComplexArr('arr push 的数据')

Access a reference data type (object)

function obj() {
    
    
    let mySymbol = Symbol()  /* es6 出现的新的基本数据类型,我只是想要使用一下 */
    let obj = {
    
    
        name: '张三',
        age: 18,
        sex: '男',
        [mySymbol]: 'symbol数据'
    }
    function getObj(){
    
    
        let newobj = {
    
    }
        Object.assign(newobj,obj)
        return newobj
    }
    function setObj(setObj){
    
    //setObj应该是个对象
        //Object.assign(obj,setObj)
        //对象只有一级属性时,用这个方法深拷贝没有问题
        //在对象的二级属性为引用类型时,这个方法就是浅拷贝二级属性了了
        //更改
        return JSON.parse(JSON.stringify(obj ))
    }
    return {
    
    
	    getObject: getObj,
	    setObject: setObj,
    }
}
let objOperate = obj()
objOperate.getObject()
objOperate.setObject({
    
    add: '新增数据'})
console.log(objOperate.getObject())

This is different from basic type data. If arr (this is an address) is directly exposed in the obtained function, it does not protect it at all. The reason is that the reference type is stored in memory.

The basic data storage is directly stored in the stack, where the misoperation will not affect the value of the protected a. The
reference data type is stored in the heap. If the protected arr is returned directly, the misoperation will change the value of arr, and if this is the operation, Make a deep copy, even if there is an external misoperation, it will not affect the protected arr because it is the arr substitute newArr that has been changed

Object.assign
replication principle, the internal implementation may be like this

//由上面的错误的思考
Object.assign = function(newObj,oldObj) {
    
    
	for(let key in oldObj) {
    
    
		newObj[key]=oldObj[key]
	}
	return newObj
}


Insert picture description here

Personal opinion, soon after getting started, if there are errors, please correct me.

Guess you like

Origin blog.csdn.net/Chennfengg222/article/details/104659620