我目前正在學習模塊化JavaScript。我不知道爲什麼我在對象中的功能沒有被識別。這是用於人員申請的列表。有人可以看看嗎? 這是我people.js文件爲什麼我的功能不被識別?
(() => {
var people = {
people: ['Will', 'Laura'],
init:() => {
this.cacheDom();
this.bindEvents();
this.render();
},
cacheDom:() => {
this.$el = $('#peopleModule');
this.$button = this.$el.find('button');
this.$input = this.$el.find('input');
this.$ul = this.$el.find('ul');
this.template = this.$el.find('#people-template').html();
},
bindEvents:()=>{
this.$button.on('click', this.addPerson.bind(this));
},
render:() =>{
var data = {
people: this.people,
};
this.$ul.html(Mustache.render(this.template,data));
},
addPerson:() => {
this.people.push(this.input.val());
this.render();
}
};
people.init();
})();
這裏是我的html
<!DOCTYPE>
<html>
<head>
<title>Nate</title>
</head>
<body>
<div>
<h1>This is my Wonderful application</h1>
</div>
<div id="peopleModule">
<h1>People</h1>
<input placeholder="name" type="text">
<button id="addPerson">Add Person</button>
<ul id="people">
<script id="people-template" type="text/template">
{{#people}}
<li>
<span>{{.}}</span>
<i class="del">X</i>
</li>
{{/people}}
</script>
</ul>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.1.2/mustache.min.js"></script>
<script src="people.js" type="text/template"></script>
</body>
</html>
這將不勝感激。 JavaScript是如此令人難以置信!
你看到了什麼樣的錯誤? –