小编Ale*_*lex的帖子

了解JavaScript Prototypes的工作原理

我正在搞乱原型,以便更好地了解它们的工作原理.我无法理解为什么我不能调用hideHeader,而我可以访问一个变量(this.header.el)

function App() {
    this.init();
    this.el = document.getElementById('box');
}

App.prototype.init = function () {
    document.write('hello world');

    this.header = new Header();

    this.header.hideHeader();
    this.header.el.style.display = 'none';
};

new App();

function Header() {
    this.el = document.getElementById('header');
}

Header.prototype.hideHeader = function() {
    this.el.style.display = 'none';
}
Run Code Online (Sandbox Code Playgroud)

javascript prototype

12
推荐指数
1
解决办法
302
查看次数

标签 统计

javascript ×1

prototype ×1