Skip to content

Instantly share code, notes, and snippets.

@congjf
Last active January 1, 2016 17:39
Show Gist options
  • Save congjf/8178296 to your computer and use it in GitHub Desktop.
Save congjf/8178296 to your computer and use it in GitHub Desktop.
Javascript Buildin Object/Function
// arguments 是Javascript内置的一个对象,使用它可以找到当前所在函数被调用时所传入的参数。使用arguments[0...i]来找到对应的参数。
// Copy from http://www.cnblogs.com/Fskjb/archive/2011/10/27/2227111.html
function format(string) {
var args = arguments;
var pattern = new RegExp(“%([1-" + arguments.length + "])”, ”g”);
return String(string).replace(pattern, function(match, index) {
return args[index];
});
};
// obj.hasOwnProperty(key):Javascript方法,用于检测对象obj中是否含有名为key让属性。
// obj.isPrototypeOf(key):Javascript方法,用于检测对象obj的原型是否有名为key的属性。
// Copy from angular.js
function sortedKeys(obj) {
var keys = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys.sort();
}
ObjMaker = function() {this.a = 'first';};
// ObjMaker is just a function, there's nothing special about it that makes
// it a constructor.
ObjMaker.prototype.b = 'second';
// like all functions, ObjMaker has an accessible prototype property that
// we can alter. I just added a property called 'b' to it. Like
// all objects, ObjMaker also has an inaccessible [[prototype]] property
// that we can't do anything with
obj1 = new ObjMaker();
// 3 things just happened
// A new, empty object was created called obj1. At first obj1 was the same as {}
// The [[prototype]] property of obj1 was set to a copy of the prototype property
// of ObjMaker. The ObjMaker function was executed, with obj1 in place of this
// ...so obj1.a was set to 'first'
obj1.a;
// returns 'first'
obj1.b;
// obj1 doesn't have a property called 'b', so JavaScript checks
// its [[prototype]]. Its [[prototype]] is the same as ObjMaker.prototype
// ObjMaker.prototype has a property called 'b' with value 'second'
// returns 'second'
// ------------------------------------------------------------------------------------
SubObjMaker = function () {};
SubObjMaker.prototype = new ObjMaker(); // Notice that this pattern is deprecated!
// Because we used 'new', the [[prototype]] property of SubObjMaker.prototype
// is now set to a copy of ObjMaker.prototype
SubObjMaker.prototype.c = 'third';
obj2 = new SubObjMaker();
// [[prototype]] property of obj2 is now set to a copy of SubObjMaker.prototype
// Remember that the [[prototype]] property of SubObjMaker.prototype
// is a copy of ObjMaker.prototype with the additional c property defined
// So now obj2 has a prototype chain!
// obj2 ---> SubObjMaker.prototype ---> ObjMaker.prototype
obj2.c;
// returns 'third', from SubObjMaker.prototype
obj2.b;
// returns 'second', from ObjMaker.prototype
obj2.a;
// returns 'first', from SubObjMaker.prototype, because SubObjMaker.prototype
// was created with the ObjMaker function, which assigned a for us
typeof object[method] !== 'function'
Javascript Buildin Object/Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment