使用 Javascript (CodeCademy) 制作更好的联系人列表显示
Making A Better Contact List Display with Javascript (CodeCademy)
我很难弄清楚以有组织的方式使输出显示的方法。如何更改输出以使其根据图片显示?
- 丑陋的显示
- 漂亮的显示
我的代码
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Wang",
number: "0000000001",
nationality: "Australian",
address: ['Street','Microsoft','California','SL','98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Wozniak",
number: "0021221312",
nationality: "American",
address: ['Street','Apple','California','SL','98052']
};
var list = function(obj){
for(var check in obj){
console.log(check);
}
};
var search = function(name){
for(var prop in friends){
if(friends[prop].firstName === name) {
console.log(friends[prop]);
return friends[prop];
}
}
};
list(friends);
search("Bill");
将
对象直接登录到控制台,将根据浏览器的内置机制记录它。 每个浏览器都会以自己的方式输出对象,所以如果你想让它看起来漂亮,你需要创建一个自定义函数,你可以根据自己的需要打印所有内容。
您可以使用扩展方法创建一个名为"Friend"的类,PrintFriend()
您可以在其中输出所有相关信息,并从中实例化您的朋友:
var friends = {};
//this is our class declaration
var Friend = function (firstName, lastName, number, nationality, address) {
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
this.nationality = nationality;
this.address = address;
};
//a prototype function that will be available to all objects of
//class 'Friend'
Friend.prototype.PrintFriend = function () {
console.log("First Name: " + this.firstName);
console.log("Last Name: " + this.lastName);
console.log("Number: " + this.number);
console.log("Address: " + this.address.join().replace(/,/g," "));
document.body.innerHTML+="First Name: " + this.firstName+"<br/>";
document.body.innerHTML+="Last Name: " + this.lastName+"<br/>";
document.body.innerHTML+="Number: " + this.number+"<br/>";
document.body.innerHTML+="Address: " + this.address.join().replace(/,/g," ")+"<br/>";
};
//we add an object named 'bill' to the friends array, the object will
//be instantiated from the Friend class
friends["bill"] = new Friend(
"Bill",
"Wang",
"0000000001",
"Australian", ['Street', 'Microsoft', 'California', 'SL', '98052']);
//same with steve
friends["steve"] = new Friend(
"Steve",
"Wozniak",
"0021221312",
"American", ['Street', 'Apple', 'California', 'SL', '98052']);
var list = function (obj) {
for (var check in obj) {
console.log(check);
}
};
//we call the custom function print friend here
var search = function (name) {
for (var prop in friends) {
if (friends[prop].firstName === name) {
friends[prop].PrintFriend();
return friends[prop];
}
}
};
list(friends);
search("Bill");
我会直接编写一个函数而不是使用控制台.log。像这样:
var logFriend = function(friend){
console.log("First Name: " + friend. firstName);
console.log("Last Name: " + friend. lastName);
console.log("Number: (" + friend.number.substring(0, 2) + ") " + friend.number.substring(3, 9));
}
相关文章:
- 有没有更好的动手、具体的方法来学习Javascript
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 在JavaScript中拆分日期字符串的更好方法是什么
- 为什么$.brower被弃用?还有什么更好的替代方案
- 设置嵌套对象属性的更好方法
- 您有更好的动态方式来缩短复杂的代码jquery吗
- 用Javascript重新格式化复杂文本日期字符串的更好方法
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何创建更好的方式来维护基于我的代码访问的最后一个页面
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- 什么更好?使用iframe或jQuery之类的东西在外部网站中加载HTML文件
- 将数组从javascript格式化为php的更好方法,反之亦然
- jquery:将动画绑定到滚动条位置的更好方法
- 有更好的方法吗?(递归解析HTML unicode实体)
- 我应该如何将响应数据保存在对象(json)中以获得更好的操作和性能
- 有什么比document.execCommand更好的东西吗
- 使用较少代码隐藏和显示选择菜单内容的更好方法是什么?javascript
- 编写if-else语句的更好方法
- 我能从“;输入“;事件是否有更好的方法来跟踪文本更改
- 使用 Javascript (CodeCademy) 制作更好的联系人列表显示