Home  >  Article  >  Backend Development  >  Why can't JS private variables be accessed?

Why can't JS private variables be accessed?

WBOY
WBOYOriginal
2016-08-23 09:02:18784browse
<code>function Customer(name) {  
    var risk = 0;  
    this.name = name;  
}  
var customer = new Customer("aa"); 
console.log(customer.name);  // aa
console.log(customer.risk); // undefined  </code>

Excuse me, why is customer.risk inaccessible but customer.name is accessible? Whose private variable is risk? Is it customer's? If so, why can't customer access his own private variable?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn