lib/mongodb/bson/lib/bson/objectid.js
function ObjectID(id) {
  // Duck-typing to support ObjectId from different npm packages
  if (id instanceof ObjectID) return id;
  if (!(this instanceof ObjectID)) return new ObjectID(id);

  this._bsontype = 'ObjectID';

  // The most common usecase (blank id, new objectId instance)
  if (id == null || typeof id === 'number') {
    // Generate a new id
    this.id = this.generate(id);
    // If we are caching the hex string
    if (ObjectID.cacheHexString) this.__id = this.toString('hex');
    // Return the object
    return;
  }

  // Check if the passed in id is valid
  var valid = ObjectID.isValid(id);

  // Throw an error if it's not a valid setup
  if (!valid && id != null) {
    throw new Error(
      'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
    );
  } else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
    return new ObjectID(new Buffer(id, 'hex'));
  } else if (valid && typeof id === 'string' && id.length === 24) {
    return ObjectID.createFromHexString(id);
  } else if (id != null && id.length === 12) {
    // assume 12 byte string
    this.id = id;
  } else if (id != null && id.toHexString) {
    // Duck-typing to support ObjectId from different npm packages
    return id;
  } else {
    throw new Error(
      'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
    );
  }

  if (ObjectID.cacheHexString) this.__id = this.toString('hex');
}
function ObjectID(id) {
  // Duck-typing to support ObjectId from different npm packages
  if (id instanceof ObjectID) return id;
  if (!(this instanceof ObjectID)) return new ObjectID(id);

  this._bsontype = 'ObjectID';

  // The most common usecase (blank id, new objectId instance)
  if (id == null || typeof id === 'number') {
    // Generate a new id
    this.id = this.generate(id);
    // If we are caching the hex string
    if (ObjectID.cacheHexString) this.__id = this.toString('hex');
    // Return the object
    return;
  }

  // Check if the passed in id is valid
  var valid = ObjectID.isValid(id);

  // Throw an error if it's not a valid setup
  if (!valid && id != null) {
    throw new Error(
      'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
    );
  } else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
    return new ObjectID(new Buffer(id, 'hex'));
  } else if (valid && typeof id === 'string' && id.length === 24) {
    return ObjectID.createFromHexString(id);
  } else if (id != null && id.length === 12) {
    // assume 12 byte string
    this.id = id;
  } else if (id != null && id.toHexString) {
    // Duck-typing to support ObjectId from different npm packages
    return id;
  } else {
    throw new Error(
      'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
    );
  }

  if (ObjectID.cacheHexString) this.__id = this.toString('hex');
}
function ObjectID ( id ) { // Duck-typing to support ObjectId from different npm packages if ( id instanceof ObjectID ) return id ; if ( ! ( this instanceof ObjectID ) ) return new ObjectID ( id ) ; this . _bsontype = 'ObjectID' ; // The most common usecase (blank id, new objectId instance) if ( id == null || typeof id === 'number' ) { // Generate a new id this . id = this . generate ( id ) ; // If we are caching the hex string if ( ObjectID . cacheHexString ) this . __id = this . toString ( 'hex' ) ; // Return the object return ; } // Check if the passed in id is valid var valid = ObjectID . isValid ( id ) ; // Throw an error if it's not a valid setup if ( ! valid && id != null ) { throw new Error ( 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' ) ; } else if ( valid && typeof id === 'string' && id . length === 24 && hasBufferType ) { return new ObjectID ( new Buffer ( id , 'hex' ) ) ; } else if ( valid && typeof id === 'string' && id . length === 24 ) { return ObjectID . createFromHexString ( id ) ; } else if ( id != null && id . length === 12 ) { // assume 12 byte string this . id = id ; } else if ( id != null && id . toHexString ) { // Duck-typing to support ObjectId from different npm packages return id ; } else { throw new Error ( 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' ) ; } if ( ObjectID . cacheHexString ) this . __id = this . toString ( 'hex' ) ; }
Create a new ObjectID instance

@class
@param {(string|number)} id Can be a 24 byte hex string, 12 byte binary string or a Number.
@property {number} generationTime The generation time of this ObjectId instance
@return {ObjectID} instance of ObjectID.
Create a new ObjectID instance