如何使一个方法工作- Javascript
how to make a method work - Javascript
我想添加以下方法到我的对象:
" hello -此方法向控制台记录字符串" hello,我的名字是"后面跟着演员的名字。"
有人能帮我找到我在哪里错了这个代码?:
function Person (firstName, lastName, age, numOscars) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.numOscars = numOscars;
hello : function greeting() {
console.log ("Hello, my name is " + this.firstName);
}
}
var actors = new Array ();
actors[0] = new Person ("Leonardo", "DiCaprio", 41, 1);
actors[1] = new Person ("Jennifer", "Lawrence", 25, 1);
actors[2] = new Person ("Samuel L."," Jackson", 67, false);
actors[3] = new Person ("Meryl", "Streep", 66, 3);
actors[4] = new Person ("John", "Cho", 43, false);
Person.hello();
将hello
改为
this.hello=function(){
//Rest of code
}
遍历actor并调用hello
方法
actors.forEach(function(item) {
item.hello();
})
JSFIDDLE
首先定义函数
function sayHello (firstName, lastName, age, numOscars) {
console.log("Hello my name is ", firstName," ", lastName, "I am", age, "years old, and I have ", numOscars, " Oscar(s).");
}
像这样创建你的演员数组,你可以添加更多的对象到它。
var actors = [{firstName: "Leonardo", lastName: "DiCaprio", age: "42", numOscars: 1}, {firstName: "example", lastName: "something", age: "12", numOscars: 0}];
,你可以单独调用你的函数,像
var leonardo = actors[0]
sayHello(leonardo.firstName, leonardo.lastName, leonardo.age, leonardo.numOscars);
或使用forEach:
actors.forEach(function(actor) {
sayHello(actor.firstName, actor.lastName, actor.age, actor.numOscars);
console.log(''n');
}
function Person(firstName, lastName, age, numOscars) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.numOscars = numOscars;
}
// If hello function is added to Person functions prototype
// then each time a new Person object is created,
// hello function gets inherited rather than creating a new hello function
Person.prototype.hello = function greeting() {
// here firstName always refer to the specific object to which belongs
console.log("Hello, my name is " + this.firstName);
}
// instead of new Array()
// Use short hand form of Array declaration also called Array literal notation
var actors =[];
// create a new person object and add it to actors array
actors.push(new Person("Leonardo", "DiCaprio", 41, 1));
actors.push(new Person("Jennifer", "Lawrence", 25, 1));
actors.push(new Person("Samuel L."," Jackson", 67, false));
actors.push(new Person("Meryl", "Streep", 66, 3));
actors.push(new Person("John", "Cho", 43, false));
// loop through each of actors array members and call
// hello function
actors.forEach(function(actor){
actor.hello();
});
您可能是初学者:)您将对象文字表示法与构造函数混合在一起。构造函数的正确版本在
下面function Person (firstName, lastName, age, numOscars) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.numOscars = numOscars;
this.hello = function () {
console.log ("Hello, my name is " + this.firstName);
}
}
相关文章:
- 在表单提交将DOM的一部分替换为分部之后,我应该将ajax成功绑定到什么来使我的javascript工作
- 我可以'不要让Javascript工作不正常
- 如何让我的html5功能不兼容警告使用javascript工作
- Excel Web Services 电子邮件 JavaScript 工作表
- 用于Google Fusion Table层的Javascript工作,但试图整理代码
- Bootstrap:如何附加一个类,并且仍然有BS Javascript工作
- JavaScript 工作队列
- JavaScript工作,直到我添加这个&&陈述
- 为什么不'在CasperJS中没有简单的JavaScript工作
- 如何"递归AJAX回调”;在JavaScript工作中
- 基本验证javascript工作不正常
- 需要帮助让这个字符串的Javascript工作
- HTML和JavaScript工作不正常
- 下拉菜单无法从CSS或Javascript工作
- 需要从erb生成一个非常特定的html来让javascript工作
- 如果javascript代码段未使用,它将阻止其他javascript工作
- 将内联事件处理程序移动到chrome扩展的javascript工作表
- 阻止其他Javascript工作的Javascript
- iframe阻止javascript工作
- 无法获取切换函数(JavaScript工作)