Understanding how JavaScript Prototypes work - Stack Overflow

Am messing around with prototypes to get a better understanding of how they work. I can't work out

Am messing around with prototypes to get a better understanding of how they work. I can't work out why I can't call hideHeader, whereas I can access a variable (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';
}

Am messing around with prototypes to get a better understanding of how they work. I can't work out why I can't call hideHeader, whereas I can access a variable (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';
}
Share Improve this question edited Mar 21, 2018 at 5:16 Glenn Ferrie 10.4k3 gold badges44 silver badges76 bronze badges asked May 17, 2015 at 19:51 AlexAlex 1771 silver badge7 bronze badges 1
  • 2 Observe what happens when you move the call to App() at the bottom, and also note that document.write overwrites the document – adeneo Commented May 17, 2015 at 19:57
Add a ment  | 

2 Answers 2

Reset to default 9

You should reorder your code so that you define hideHeader before you try to invoke it.

Like this:

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

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

Header.prototype.hideHeader = function() {
    this.el.style.display = 'none';
}

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

    this.header = new Header();

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

new App();

JavaScript is an interpreted language, it's not piled. It is evaluated sequentially as it is loaded into memory.

You just need to change the order of how you are doing things. For example:

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


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

Header.prototype.hideHeader = function() {
    this.el.style.display = 'none';
}

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

    this.header = new Header();

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

new App();
<div id="header"></div>
<div id="box"></div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745535934a4631903.html

相关推荐

  • Understanding how JavaScript Prototypes work - Stack Overflow

    Am messing around with prototypes to get a better understanding of how they work. I can't work out

    23小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信