0

这是我的代码。基本上,我正在创建一个对象,它的一些属性是由实例化对象后需要运行的函数设置的。我正在 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)
}
4

2 回答 2

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);

第一个选项更具原子性,并且对调用者进行所有正确的初始化调用的负担更小,但是如果有多种不同的方法来初始化对象,则第二个选项更加灵活。我更喜欢前者,因为通常最好减少调用者搞砸的方式,但有时第二个选项的灵活性很有用。

于 2013-01-11T18:15:41.857 回答
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');
    }
于 2013-01-11T19:54:04.753 回答