简体   繁体   English

关于对象和函数的用法的困惑

[英]confusion on the usage of object and function javascript

especially the 'this' keyword. 尤其是“ this”关键字。 Like below code, using function, I can avoid duplication of code already. 像下面的代码一样,使用函数,我可以避免重复代码。 The more I read sample code the more confuse I'm, it's like things can be achieved this way, but there are other (complex) ways to do it.. or I'm wrong? 我阅读示例代码的时间越长,我就越困惑,这就像可以通过这种方式来实现,但是还有其他(复杂的)方式可以实现..还是我错了?

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"
};

// printPerson added here
function printPerson(person){
    console.log(person.firstName + " " + person.lastName);

}

printPerson(bob);
printPerson(mary);

my question is, how to improve above code by using this keyword. 我的问题是,如何通过使用this关键字来改进上述代码。 For now, I already seeing OOP (or I'm wrong?). 现在,我已经看到了OOP(或者我错了吗?)。

extra : Need no constructor or something more complex like new keyword. extra:不需要构造函数或诸如new关键字之类的更复杂的东西。

function Person(firstName, lastName, phoneNumber, eMail) {
    var that = this;
    that.firstName = firstName;
    that.lastName = lastName;
    that.phoneNumber = phoneNumber;
    that.eMail = eMail;
    return {
        printPerson: function() {
            console.log(that.firstName + " " + that.lastName);
        }
    }
};

var person1 = Person("Bruce", "Wayne", "1234", "bane@joker.com");
person1.printPerson();

var person2 = Person("Kent", "Clark", "4321", "me@thesky.com");
person2.printPerson();

The problem of the printPerson approach is when you need "polymorphism"; printPerson方法的问题在于何时需要“多态性”。 ie you want to print an object but you don't want to know what kind of object it is: suppose 也就是说,您想打印一个对象,但又不想知道它是哪种对象:假设

function newPerson(firstName, lastName) {
    return { firstName: firstName,
             lastName: lastName };
}

function newPet(name, kind) {
    return { name: name,
             kind: kind };
}

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

function printPet(pet) {
    console.log(pet.name + " (a nice " + pet.kind + ")");
}

var p1 = newPerson("Andrea", "Griffini");
var p2 = newPet("Tobi", "cat");

printPerson(p1);
printPet(p2);

the problem however is... suppose you're given an object x and you need to print it, but you don't know if it's a person or a pet... how do you do? 但是问题是...假设您给了一个对象x并且需要打印它,但是您不知道它是人还是宠物...怎么办? Solution 1 is checking with if / then 解决方案1正在检查if / then

if (x.firstName) {
    printPerson(x);
} else {
    printPet(x);
}

but this is annoying because if you add later another type of object you need to fix that. 但这很烦人,因为如果以后添加另一种类型的对象,则需要修复该问题。

The OOP solution is to change the code slightly OOP解决方案是稍微更改代码

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.print = function() {
        console.log(this.firstName + " " + this.lastName);
    };
}

function Pet(name, kind) {
    this.name = name;
    this.kind = kind;
    this.print = function() {
        console.log(this.name + " (a nice " + this.kind + ")");
    };
}

and now the use becomes 现在用途变成

var p1 = new Person("Andrea", "Griffini");
var p2 = new Pet("Tobi", "cat");

p1.print();
p2.print();

and the problem for a general x is just x.print() . 而一般x的问题只是x.print()

Basically the idea is to keep the data (eg firstName , lastName ) and the code (eg the implementation of print ) together for each type of object. 基本上,想法是将每种类型的对象的数据(例如firstNamelastName )和代码(例如print的实现)保持在一起。 You can add a new object type by just implementing the constructor and without the need to change other code that will "magically" end up calling the correct version of print . 您可以通过实现构造函数来添加新的对象类型,而无需更改将“神奇地”结束调用正确版本的print其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM