js0018 发表于 2018-12-15 | 阅读次数: 字数统计: 148 | 阅读时长 ≈ 1 通过函数访问私有变量 而不是通过对象访问 12345678910111213141516171819202122232425262728293031<!DOCTYPE html><html><head> <title>Private variables are accessed through functions and not through objects</title> <meta charset="utf-8"> <script src="../assert.js"></script> <link rel="stylesheet" type="text/css" href="../assert.css"></head><body> <script> function Ninja() { var feints = 0; this.getFeints = function(){ return feints; }; this.feint = function(){ feints++; }; } var ninja1 = new Ninja(); ninja1.feint(); var imposter = {}; imposter.getFeints = ninja1.getFeints; assert(imposter.getFeints () === 1, "The imposter has access to the feints variable!"); </script></body></html>