0

我正在学习 javascript 中的对象,我正在使用一个函数来构造一个对象并向它添加一个方法。很明显,有一种方法可以更改对象的 firstName,但是 this.changeName=changeName;行是什么?
究竟是做什么的?如果我删除它或将 changeName 函数名称更改为其他名称,则会发生错误并且不显示任何内容。并且删除这行代码也会导致错误,所以看起来代码运行是必不可少的,但我真的不知道它做了什么。

<script>

function person(firstName, lastName, age){
    this.firstName=firstName;
    this.lastName=lastName;
    this.age=age;
    this.changeName=changeName;

    function changeName(name){
        this.firstName=name;
    }
}
me = new person("Hazem", "Khadash", 18);
me.changeName("Bashar");
document.write(me.firstName);

根据我对代码的理解,创建了 me,将 changeMe() 函数作为方法调用,然后将 person.lastName 呈现在屏幕上。

谢谢。

4

5 回答 5

2

您正在使changeName可访问性成为person. 将其视为暴露changeName

没有它,您将无法做到me.changeName,因为me.changeName不再存在于person.

于 2013-08-28T20:07:53.337 回答
1
function person(firstName, lastName, age) {
    ...

    function changeName(name){
        this.firstName=name;
    }
}

创建一个仅在函数内部可用的函数person。正如您所发现的那样,只有这样是me.changeName(…)行不通的,因为该功能无法在外部访问person

this.changeName = changeName

使该函数可在person函数外部访问,以便您me.changeName稍后调用。

注意是this.changeName = changeName,不是this.changeName = changeName()。使用括号,它将调用函数并将其输出分配给this.changeName; 没有括号,它将对函数的引用changeName分配给this.changeName.

你可以把它改成这个,它是等价的:

function person(firstName, lastName, age) {
    ...
    this.changeName = function(name){
        this.firstName=name;
    }
}

编写“类”的最佳方式person可能是这样的:

function person(firstName, lastName, age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
}

person.prototype.changeName = function changeName(name){
    this.firstName = name;
};
于 2013-08-28T20:09:49.517 回答
1

As you might have already heard in Javascript everything is an object. As you are within the function person you are "in the object" person as well. So this refers to the oject. Declaring a nested function within this function makes it only visible in the context of the function and not anywhere else. To make it available to the outer world you have to assign it to an object field, otherwise you can't call it from outside. You can think of it like a method of a class (there are no real classes in JS) - if you are familiar with object oriented programming.

The same thing would be:

function person(firstName, lastName, age){
  this.firstName=firstName;
  this.lastName=lastName;
  this.age=age;
  this.changeName = function(name){
    this.firstName=name;
  }
}

When you call this method within the scope of an instance of person. "this" always refers to that particular instance.

One could summarize:

this.changeName=changeName; -> assigns the still undefined local object changeName to the object-field changeName

function changeName(name) -> declares the local object changeName as a function.

Remember - in JavaScript everything is an object functions, variables, arrays,...

于 2013-08-28T20:24:35.363 回答
0

在 javascript 中,函数可以作为数字或字符串分配给变量。在这种特殊情况下,一个person名为的属性changeName被分配了 function changeNamechangeName这个赋值允许你通过这个 proberty ( me.changeName("Bashar");)调用函数

于 2013-08-28T20:08:48.097 回答
0

如果没有那行代码,您可以从person 对象的范围内调用 changeName(name) 函数。但是使用那行代码,您可以从范围之外调用该函数,例如底部的行:me.changeName("Bashar")。

于 2013-08-28T20:12:30.693 回答