简体   繁体   English

为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性'john'

[英]Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined

I am new to JavaScript and may not be using correct terms so please bear with me. 我是JavaScript的新手,可能没有使用正确的术语,所以请耐心等待。

http://jsbin.com/IPaDAJO/1/ http://jsbin.com/IPaDAJO/1/

I am expect the following be displayed on the page Hello my name is John Smith and I am 29 years old. 我希望页面上显示以下内容您好我的名字是John Smith,我今年29岁。 Hello my name is Suzie Alport and I am 24 years old. 您好,我的名字是Suzie Alport,我今年24岁。

I want to understand why can't Suzie refer to John's greet method? 我想明白为什么Suzie不能提到John的问候方法?

<script>
        var employees = {
            john: {
                name: "John Smith",
                age: 29,
                job: "web developer",
                greet: function(){
                    document.write("Hello my name is " + this.name + " and I am " + this.age + " years old. ");
                }
            },
            suzie: {
                name: "Suzie Alport",
                age: 24,
                job: "nursery assistant",
                greet: employees.john.greet
            }
        };
        employees.john.greet();
        employees.suzie.greet();
</script>

Thanks 谢谢

As of the time that you're creating the object you're assigning to the suzie property, employees is undefined . 当您创建要分配给suzie属性的对象时, employeesundefined The property initializers are evaluated before the overall expression is complete. 在整个表达式完成之前评估属性初始值设定项。

Here's the order in which things happen above: 以下是事情发生的顺序:

  1. The engine creates a variable called employees with the initial value undefined . 引擎创建一个名为employees的变量,其初始值为undefined
  2. The engine creates a blank object (which will eventually be assigned to employees ). 引擎创建一个空白对象(最终将分配给employees )。
  3. The engine creates another blank object (which will eventually become the john property value). 引擎创建另一个空白对象(最终将成为john属性值)。
  4. It evaluates "John Smith" and creates a property on the blank object from #3 called name , giving it that value. 它评估"John Smith"并在#3名为name的空白对象上创建一个属性,为其赋予该值。
  5. It repeats that process for age , job , and greet . 它重复了agejobgreet
  6. It assigns that object to the john property on the object from #2 above. 它将该对象分配给上面#2对象的john属性。
  7. It creates another blank object (to become suzie ). 它创建了另一个空白对象(变成suzie )。
  8. It evaluates "Suzie Alport" and creates a property on the object from #7 called name , giving it that value. 它评估"Suzie Alport"并在#7 name为对象创建一个属性,赋予它该值。
  9. It repeats the process for age and job . 它重复了agejob的过程。
  10. It tries to evaluate the expression employees.john.greet and fails because employees is undefined . 它试图评估employees.john.greet表达式并因为employees undefined而失败。

If you break it into two pieces, it works (until document.write blows things up): 如果你把它分成两部分,它可以工作(直到document.write搞砸了):

var employees = {
    john: {
        name: "John Smith",
        age: 29,
        job: "web developer",
        greet: function(){
            document.write("Hello my name is " + this.name + " and I am " + this.age + " years old. ");
        }
    }
};
employees.suzie = {
    name: "Suzie Alport",
    age: 24,
    job: "nursery assistant",
    greet: employees.john.greet
};
employees.john.greet();
employees.suzie.greet();

That works because before we try to evaluate employees.john.greet , a reference to the object created in #2 above has been assigned to the employees variable. 这是有效的,因为在我们尝试评估employees.john.greet之前,已将对上面#2中创建的对象的引用分配给employees变量。


Side note: 边注:

Avoid using document.write in modern web pages and applications. 避免在现代网页和应用程序中使用document.write Instead, use DOM methods . 相反,使用DOM方法 document.write only works the way you likely want it to during the initial page load; document.write仅在初始页面加载期间以您希望的方式工作; after initial page load, it will wipe out the existing document entirely and start writing a new document in the same window. 在初始页面加载后,它将完全擦除现有文档并开始在同一窗口中编写新文档。

For instance, you can append paragraphs to the document.body like this: 例如,您可以将段落附加到document.body如下所示:

function display(msg) {
  var p = document.createElement('p');
  p.innerHTML = String(msg);
  document.body.appendChild(p);
}

Live Example with both of the above ( source ) 以上两者的实例来源

暂无
暂无

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

相关问题 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标题'”? - Why am i getting this error: “Uncaught TypeError: Cannot read property 'Title' of undefined”? 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 为什么我收到“Select.js:1555 Uncaught TypeError:无法读取未定义的属性‘isSelectOptGroup’” - Why I am getting "Select.js:1555 Uncaught TypeError: Cannot read property 'isSelectOptGroup' of undefined" 为什么我会收到“未捕获的类型错误:无法读取未定义的属性 &#39;body&#39;”? - Why am I getting "Uncaught TypeError: Cannot read property 'body' of undefined"? 为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”? - why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign? 为什么会出现Uncaught TypeError:无法读取未定义的属性&#39;displayQuestion&#39;? - Why am i getting Uncaught TypeError: Cannot read property 'displayQuestion' of undefined? 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property &#39;initializeApp&#39; of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 为什么我得到 Uncaught (in promise) TypeError: Cannot read property &#39;type&#39; of undefined? - Why am I getting Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? 我收到错误,因为 Uncaught TypeError: Cannot read property &#39;use&#39; of undefined - I am getting error as Uncaught TypeError: Cannot read property 'use' of undefined 运行此程序后出现错误,Uncaught TypeError:无法读取未定义的属性“ push” - I am getting error after run this program, Uncaught TypeError: Cannot read property 'push' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM