简体   繁体   English

这行Js代码到底在做什么?

[英]What is this line of Js code exactly doing?

I am learning about objects in javascript and I'm using a function to construct an object and adding a method to it. 我正在学习JavaScript中的对象,并且正在使用一个函数来构造对象并向其中添加方法。 So clearly there's a method to change the firstName of the object, but what does the line this.changeName=changeName; 很明显,有一种方法可以更改对象的名字,但是this.changeName = changeName;行的内容是什么
exactly do? 到底是做什么的? If I delete it or alter the changeName function name to something else an error occurs and nothing is displayed. 如果删除它或将changeName函数名称更改为其他名称,则会发生错误,并且不会显示任何内容。 And deleting this line of code also leads to an error, so it seems it's essential for the code to run but I really can't figure out what it does. 删除这一行代码也会导致错误,因此看来运行代码至关重要,但我真的不知道它能做什么。

<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);

As I understand the code, me is created, changeMe() function is called as a method then person.lastName is rendered on the screen. 据我了解的代码,创建了我,将changeMe()函数作为方法调用,然后将person.lastName呈现在屏幕上。

Thanks. 谢谢。

You are making changeName accessible as part of person . 您正在将changeName作为person一部分进行访问。 Consider it as exposing changeName . 将其视为公开changeName

Without it, you cannot do me.changeName as me.changeName no longer exists outside of the scope of person . 没有它,您将无法执行me.changeName因为me.changeName不再存在于person范围之外。

function person(firstName, lastName, age) {
    ...

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

creates a function that is only available inside the function person . 创建一个仅在功能person内部可用的功能。 As you've found, having only that makes me.changeName(…) not work, because the function isn't accessible outside person . 正如您所发现的那样,只有那样会使me.changeName(…)无法正常工作,因为该函数无法在person外部访问。

this.changeName = changeName

makes that function accessible outside the person function so you can call me.changeName later. 使该功能可以在person功能之外访问,因此您以后可以调用me.changeName

Notice it's this.changeName = changeName , not this.changeName = changeName() . 请注意,它是this.changeName = changeName ,而不是this.changeName = changeName() With the parentheses, it would call the function and assign its output to this.changeName ; 使用括号,它将调用该函数并将其输出分配给this.changeName ; without parentheses, it assigns a reference to the function changeName to this.changeName . 不带括号,它将对功能changeName引用分配给this.changeName

You could change it to this, and it would be equivalent: 您可以将其更改为此,这将是等效的:

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

The best way to write your person "class" is probably like this: 将您的person写成“班级”的最佳方法可能是这样的:

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

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

As you might have already heard in Javascript everything is an object. 您可能已经在Javascript中听说过,所有东西都是对象。 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. 如果您熟悉面向对象的编程,则可以将其视为类的方法(JS中没有真正的类)。

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. “ this”总是指该特定实例。

One could summarize: 一个可以总结为:

this.changeName=changeName; this.changeName = changeName; -> assigns the still undefined local object changeName to the object-field changeName ->将仍未定义的本地对象changeName分配给对象字段changeName

function changeName(name) -> declares the local object changeName as a function. 函数changeName(name)->将本地对象changeName声明为一个函数。

Remember - in JavaScript everything is an object functions, variables, arrays,... 请记住-在JavaScript中,一切都是对象函数,变量,数组,...

In javascript functions can be assigned to variables as a number or a string. 在javascript中,可以将变量分配为数字或字符串。 In this particular case a person property named changeName is assigned with function changeName . 在这种特殊情况下,将名为changeNameperson属性分配给功能changeName This assignment allows you to call changeName function trough this proberty ( me.changeName("Bashar"); ) 通过此分配,您可以changeName此探测来调用changeName函数( me.changeName("Bashar");

Without that line of code, you can call the changeName(name) function from within the scope of the person object. 没有那行代码,您可以在person对象的范围调用changeName(name)函数。 But with that line of code, you can call that function from outside of the scope, such as the line towards the bottom: me.changeName("Bashar"). 但是使用该行代码,您可以从范围外部调用该函数,例如朝底部的行:me.changeName(“ Bashar”)。

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

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