Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第358天 举例说明Object.defineProperty会在什么情况下造成循环引用导致栈溢出? #2206

Open
haizhilin2013 opened this issue Apr 7, 2020 · 2 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第358天 举例说明Object.defineProperty会在什么情况下造成循环引用导致栈溢出?

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Apr 7, 2020
@a298003154
Copy link

a298003154 commented Apr 8, 2020

var data = {
    count: 1,
    value: 2
}
Object.defineProperty(data, 'count', {
    enumerable: true,
    configurable: true,
    get: function () {
        console.log('你访问了count', this.count); // 循环读取data.count 导致报错
        return this.value
    },
    set: function (newVal) {
        console.log('你设置了count');
    }
})
console.log(data.count) // 报错 Maximum call stack size exceeded

@xxx135261
Copy link

var person = {
	age:1
}
Object.defineProperty(person,'age',{
	get(){
		return this.age
	},
	set(val){
		this.age = val
	}
})

person.age   报错:Maximum call stack size exceeded,循环引用导致栈溢出

person.age --> get.call(person) -->this.age --> person.age --> get.call(person) --> this.age --> .......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

3 participants