Skip to content

question: remove required new operator #172

Description

@ryanseys

In addition to this:

dataset = new datastore.Dataset({
  projectId: 'my-project',
  keyFilename: '/path/to/keyfile.json'
});

can we support (note the lack of the new keyword):

dataset = datastore.Dataset({
  projectId: 'my-project',
  keyFilename: '/path/to/keyfile.json'
});

We can accomplish this by a technique discussed here.

Essentially detect whether the object calling it is an instance of Dataset or not:

if ( !(this instanceof Dataset) ) {
  return new Dataset();
}

If this change was to be made, I would suggest that developers omit new always and we don't document it as requiring it in the first place as it simplifies what the developer needs to worry about. The capitalization of Dataset implies its a constructor but they can call it just like a regular method on datastore.

Thoughts?

Activity

  1. stephenplusplus commented on Sep 4, 2014

    @stephenplusplus
    Contributor

    Why?

    If their codebase is using JSHint (and probably other linters), they'll have to explicitly turn off the recommendation of capital character referring to something that should be new'd. I'm in favor of continuing to use the new behavior, because hiding it is unnecessary confusion, which only has the purpose of avoiding using "new". I think it's best we stick to the convention and recommend using new since they are instantiating an object.

    I don't think it would hurt, however, to add in the if to catch non-instantiating executions.

  2. ryanseys commented on Sep 5, 2014

    @ryanseys
    ContributorAuthor

    Yeah, makes sense. I didn't know JSHint would do this.

  3. stephenplusplus commented on Sep 6, 2014

    @stephenplusplus
    Contributor

    I've come around. Me, from #168:

    It feels like forcing the developer to use new is important for them to understand "you're getting a new object that is meant to be multiply instantiated," but I suppose you can argue that that's an implementation detail of how our internal structure works, and saying gcloud.datastore.dataset(), for example, can be just as clear, especially with the help of the documentation.

    I would be interested in hearing what @rakyll and others think about facading our internal use of new, creating an end-user api like:

    var gcloud = require('gcloud');
    var dataset = gcloud.datastore.dataset({});
    var bucket = gcloud.storage.bucket({});
  4. ryanseys commented on Sep 6, 2014

    @ryanseys
    ContributorAuthor
    var gcloud = require('gcloud');
    var dataset = gcloud.datastore.dataset({});
    var bucket = gcloud.bucket({});

    😍

  5. stephenplusplus commented on Sep 6, 2014

    @stephenplusplus
    Contributor

    Oops, had to update my comment. Hope you still like it:

    -var bucket = gcloud.bucket({});
    +var bucket = gcloud.storage.bucket({});
  6. ryanseys commented on Sep 6, 2014

    @ryanseys
    ContributorAuthor

    Didn't notice that mistake. Yes, that makes more sense and still looks perfect.

  7. added a commit that references this issue on Sep 15, 2014
    611f7f2
  8. added this to the Core Stable milestone on Feb 2, 2015
  9. 69 remaining items

  10. added a commit that references this issue on Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

coretype: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions