这是我的代码。基本上,我正在创建一个对象,其中一些属性是由我实例化对象后需要运行的函数设置的。我正在Google Scripts代码编辑器中开发它,它运行时没有错误,但实际上没有做任何事情。
此功能由定时触发器
调用function addFoldersToSites(){
//DriveObject class, has a folder id and page url, and uses those to get attachments
function DriveObject(folder_id,page_url) {
this.folder_id = folder_id
this.page_url = page_url
this.files = setFiles
this.page = setPage
this.attachments = setAttachments
}
function setFiles(){return DocsList.getFolderById(this.folder_id).getFiles();}
function setPage(){return SitesApp.getPageByUrl(this.page_url);}
function setAttachments(){return this.page.getAttachments();}
//instantiate drive objects
//if you want to add drive things to new site pages, this is where you do it
//*************************************************************************
var engpage = new DriveObject('0B6esS6X9k9LvWWFfc1oyN0VfeTg','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
var productpage = new DriveObject('0B6esS6X9k9LveUI0dmxxLWplaTA','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
var adminpage= new DriveObject('0B6esS6X9k9LvVUVDUlBWWWhJcDg','https://sites.google.com/a/drawbrid.ge/resources/home/files');
var genonboarding = new DriveObject('0B6esS6X9k9LvbjRtanE4eGNkdlE','https://sites.google.com/a/drawbrid.ge/resources/home/general-onboarding');
var salespage= new DriveObject('0B6esS6X9k9LvQ2JJN0pCblNyME0','https://sites.google.com/a/drawbrid.ge/resources/sales/files');
var researchpage = new DriveObject('0B6esS6X9k9LvM1RKeHVXRkNBZ1k','https://sites.google.com/a/drawbrid.ge/resources/market-research');
//function that iterates through folder files and puts them in the google site frame
function showFolderInSite(attachments, page, files) {
var attachments = attachments
var page = page
var files = files
for (i in attachments) {
attachments[i].deleteAttachment();
}
for (i in files) {
page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
}
}
//run functions
showFolderInSite(engpage.attachments,engpage.page,engpage.files)
showFolderInSite(productpage.attachments,productpage.page,productpage.files)
showFolderInSite(adminpage.attachments,adminpage.page,adminpage.files)
showFolderInSite(genonboarding.attachments,genonboarding.page,genonboarding.files)
showFolderInSite(salespage.attachments,salespage.page,salespage.files)
showFolderInSite(researchpage.attachments,researchpage.page,researchpage.files)
}
答案 0 :(得分:2)
实例化具有所有属性的对象的唯一原子方法是将它们全部传递给构造函数。
然后,构造函数可以使用这些参数来调用可能需要处理这些参数的任何其他函数,作为正确初始化的一部分。
所以,你基本上有这两个选择(第一个是更原子的初始化):
function myConstructor(a, b, c, d, e, f) {
this.whatever = a;
this.aabb = processArg(b, c);
// etc...
}
var myObj = new myConstructor(true, 3, "foo", 4, 5, 6);
或者这个:
function myConstructor() {
}
myConstructor.prototoype = {
init1: function(a, b) {},
init2: function(a, b) {},
init3: function(a, b) {},
}
var myObj = new myConstructor();
myObj.init1(true, 3);
myObj.init2("foo", 4);
myObj.init3(5, 6);
第一个选项更加原子化,减少调用者的负担以进行所有正确的初始化调用,但如果有多种不同的方法来初始化对象,则第二个选项更灵活。我更喜欢前者,因为通常更好的方法是让调用者搞砸更少的方法,但偶尔选择第二种选择的灵活性是有用的。
答案 1 :(得分:1)
我认为这正是您所寻找的(同时确保DocsList和SitesApp可以访问):
function addFoldersToSites(){
//DriveObject class, has a folder id and page url, and uses those to get attachments
function DriveObject(folder_id,page_url) {
this.folder_id = folder_id;
this.page_url = page_url;
this.showFolderInSite();
}
DriveObject.prototype = {};
DriveObject.prototype.constructor = DriveObject;
DriveObject.prototype.files = function setFiles(){return DocsList.getFolderById(this.folder_id).getFiles();}
DriveObject.prototype.page = function setPage(){return SitesApp.getPageByUrl(this.page_url);}
DriveObject.prototype.attachments = function setAttachments(){return this.page.getAttachments();}
//function that iterates through folder files and puts them in the google site frame
DriveObject.prototype.showFolderInSite = function showFolderInSite() {
var attachments = this.attachments();
var page = this.page();
var files = this.files();
for (i in attachments) {
attachments[i].deleteAttachment();
}
for (i in files) {
page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
}
}
//instantiate drive objects
//if you want to add drive things to new site pages, this is where you do it
//*************************************************************************
var engpage = new DriveObject('0B6esS6X9k9LvWWFfc1oyN0VfeTg','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
var productpage = new DriveObject('0B6esS6X9k9LveUI0dmxxLWplaTA','https://sites.google.com/a/drawbrid.ge/resources/engineering/files');
var adminpage= new DriveObject('0B6esS6X9k9LvVUVDUlBWWWhJcDg','https://sites.google.com/a/drawbrid.ge/resources/home/files');
var genonboarding = new DriveObject('0B6esS6X9k9LvbjRtanE4eGNkdlE','https://sites.google.com/a/drawbrid.ge/resources/home/general-onboarding');
var salespage= new DriveObject('0B6esS6X9k9LvQ2JJN0pCblNyME0','https://sites.google.com/a/drawbrid.ge/resources/sales/files');
var researchpage = new DriveObject('0B6esS6X9k9LvM1RKeHVXRkNBZ1k','https://sites.google.com/a/drawbrid.ge/resources/market-research');
}