这是怎么回事? JS [英] WHy does this happen? Js

查看:68
本文介绍了这是怎么回事? JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么contacts数组中只有两个项目?我猜这个问题出现在添加功能中,我正在尝试添加一个新人。



Why is there still only two items in the contacts array? I'm guessing the problem is in the "add function," i'm trying to add a new person.

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

function list() {
	var contactsLength = contacts.length;
	for (var i = 0; i < contactsLength; i++) {
		printPerson(contacts[i]);
	}
}

/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
    var contactsLength = contacts.length;
    for(i = 0;i < contacts.length; i++) {
        if(contacts[i].lastName === lastName) {
            printPerson(contacts[i]);
        }
    }
};
search("Jones")
//------------------------------
function add(firstName, lastName, email, telephone){
    //add newPerson object
    var newPerson ={
        firstName: firstName,
        lastName: lastName,
         
        email: email,
        phoneNumber: telephone
    };
    //add object to array
    contacts[contacts.length] = newPerson;
     
}



//1.4 run

list();

// -------------------------------------------------- ---------

为什么newPerson没有添加到联系人?


//1.4 run
list();
//-----------------------------------------------------------
why is newPerson not adding into contacts?

推荐答案

你的方法从一开始就错了。永远不要这样做。我会解释原因。它可能无法解决您报告的问题;我甚至不想看它,因为这首先是浪费时间。你需要修复更重要的事情。



看看你的定义 bob 。 (你总是如此不礼貌地命名不使用大写字母的人吗?这只是顺便说一下......)

定义完全正确,语法和一切。如果只有一个 bob Bob ,你可以完美地使用它。但是,在那之后,你有 mary 。可怜的玛丽破坏了一切。此时,您的代码已经死了,即使它有效。想象一下如果你拼错了至少一个属性名称会发生​​什么。找到错误太难了。以下是您要做的事情:

Your approach is wrong from the very beginning. Never do such things. I'll explain why. It may not fix the problem you are reporting; I don't even want to look at it, because it would be a waste of time, first of all. You need to fix more important thing.

Look at your definition of bob. (Do you always so impolitely name people not using capitalization? It's just by the way…)
The definition is perfectly fine, syntax and everything. You could use it perfectly, if there was only one bob, or Bob. But, after that, you have mary. Poor Mary spoils everything. At this point, your code is already dead, even if it worked. Imagine what could happened if you misspelled at least one property name. It would be too hard to locate the bug. Here is what you want to do instead:
var Person = function(firstName, lastName, phoneNumber, email) {
   this.firstName = firstName;
   this.lastName = lastName;
   this.phoneNumber = phoneNumber;
   this.email = email;
}

// ...

var contacts = [
   new Person("Mary", "Johnson", "(650) 888-8888", "mary.johnson@example.com"),
   new Person("Bob", "Jones", "(650) 777-7777", "bob.jones@example.com")
];



你能看到这个想法吗?你重复使用人的创造,避免我提到的问题。没有必要在无意义的功能添加



此外,您可以检查是否有两个对象来自相同的构造函数:


Can you see the idea? You re-use creation of person and avoid the problem I mentioned. There is no need in the pointless function add.

Moreover, you can check up if two objects came from the same constructor:

var sameTypes = contacts[0].constructor === contacts[1].constructor; // will return true
// assuming your old bob definition was there:
var differentTypes = contacts[0].constructor === bob.constructor; // will return false
// even though the objects are logically equivalent;
// also, it won't throw an exception: bob object does have a constructor



不明白我对类型文学:它们既不是JavaScript意义上的类型,也不是OOP意义上的类型(JavaScript中没有任何东西真的是OOP;概念根本不同)。



请理解我的建议并不是强迫您使用不同的风格;它更为基础:我建议你避免如此残酷地违反更基本的原则:不要重复自己: http:// en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ]。



请从这一点开始,看看你是否可以应对这个简单的问题和其他一切。



-SA


这篇关于这是怎么回事? JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆