Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript __谷歌应用程序脚本中的原型?_Javascript_Google Apps Script - Fatal编程技术网

Javascript __谷歌应用程序脚本中的原型?

Javascript __谷歌应用程序脚本中的原型?,javascript,google-apps-script,Javascript,Google Apps Script,编程新手…我试图学习JavaScript中的对象继承 我得到以下代码的一个错误。它说: TypeError:在对象[object]中找不到函数getName \uuuu proto\uuuu(又称“dunder proto”)在应用程序脚本中不起作用吗?如果没有默认的“对象”,如何将继承设置为其他对象 function onPlay(){ //create an employee constructor function Emp(last, first){ this.first = fir

编程新手…我试图学习JavaScript中的对象继承

我得到以下代码的一个错误。它说:

TypeError:在对象[object]中找不到函数getName

\uuuu proto\uuuu
(又称“dunder proto”)在应用程序脚本中不起作用吗?如果没有默认的“对象”,如何将继承设置为其他对象

function onPlay(){

//create an employee constructor
function Emp(last, first){
  this.first = first;
  this.last = last;
  this.getName = function() {return(this.first+this.last);}
}

//create an employee
var emp1 = new Emp("Halpert", "Jim");

//log the employee's name
Logger.log(emp1.getName());


//create a manager constructor  
function Mgr(){
  this.salary = 100,000;
}

//managers are also employees
Mgr.__proto__ = Emp.prototype;

//create a manager
var mgr1 = new Mgr("Scott", "Michael");

//log the manager's name
Logger.log(mgr1.getName());  
}
而不是:

Mgr.__proto__ = Emp.prototype;
您可能想要:

Mgr.prototype = Object.create(Emp);
\uuuuu proto\uuuu
属性用于修改原型,并不一定在所有JavaScript引擎中都可用。要为自定义对象构造函数设置原型,您需要将构造函数函数上的
prototype
对象设置为基类的实例(使用
object.create
不必调用父构造函数)。

而不是:

Mgr.__proto__ = Emp.prototype;
您可能想要:

Mgr.prototype = Object.create(Emp);

\uuuuu proto\uuuu
属性用于修改原型,并不一定在所有JavaScript引擎中都可用。要为自定义对象构造函数设置原型,您需要将构造函数函数上的
原型
对象设置为基类的实例(使用
对象。创建
时不要不必要地调用父构造函数)。

切勿使用
\u原型
,这是不推荐的!顺便说一句,您想更改
Mgr.prototype
的原型链,而不是
Mgr
构造函数的原型链。永远不要使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,它已被弃用!顺便说一句,您想更改
Mgr.prototype
的原型链,而不是
Mgr
构造函数的原型链。