JavaScript中在全局作用域定义的函数实际上只能被某个对象调用是什么意思?
phpcn_u251
phpcn_u251 2017-03-25 13:19:27
0
2
1070
  function Person(name, age, job) {
        this.name = name;
        this.age = age;
        this.job = job;
        this.sayName = sayName;
  
    }
    
    function sayName() {
        alert(this.name);
    }
    
    var person1 = new Person("Nicholas", 19, "Teacher");
    var person2 = new Person("Greg", 20, "Doctor");
phpcn_u251
phpcn_u251

reply all(2)
数据分析师

What does it mean that a function defined in the global scope in JavaScript can actually only be called by a certain object? - PHP Chinese Q&A - A function defined in the global scope in JavaScript can actually only be called by a certain object. What does it mean? - PHP Chinese website Q&A

Take a look around and learn.

阿神

比如你写了一个插件 plugin.js
想让这个插件内的对象可以被使用者调用
根据使用者的环境不同(web-browser node.js等),你的这些对象需要”附加“在一些父对象上,这样不同的环境下才可以使用(比如浏览器中window这个父对象)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template