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");
数据分析师2017-10-01 01:08:04
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.
阿神2017-03-25 13:19:46
比如你写了一个插件 plugin.js
想让这个插件内的对象可以被使用者调用
根据使用者的环境不同(web-browser node.js等),你的这些对象需要”附加“在一些父对象上,这样不同的环境下才可以使用(比如浏览器中window这个父对象)