K0II / JavaScript

Learning Logs
0 stars 0 forks source link

Object.getOwnPropertyNames() #4

Open K0II opened 8 years ago

K0II commented 8 years ago

九个内置对象(函数)原型的property

String.prototype.sayHello = function(){
    console.log('hello');
}

Object.getOwnPropertyNames(String.prototype);
//  ["length", "constructor", "toSource", "toString", "valueOf", "toLowerCase", "toUpperCase", "charAt", "charCodeAt", "substring",
//  "codePointAt", "includes", "contains", "indexOf", "lastIndexOf", "startsWith", "endsWith", "trim", "trimLeft", "trimRight",
//  "toLocaleLowerCase", "toLocaleUpperCase", "localeCompare", "repeat", "normalize", "match", "search", "replace", "split",
//  "substr", "concat", "slice", "bold", "italics", "fixed", "strike", "small", "big", "blink", "sup", "sub", "anchor", "link",
//  "fontcolor", "fontsize", "sayHello"]

Object.getOwnPropertyNames(Number.prototype);
//  ["constructor", "toSource", "toString", "toLocaleString", "valueOf", "toFixed", "toExponential", "toPrecision"]

Object.getOwnPropertyNames(Boolean.prototype);
//  ["constructor", "toSource", "toString", "valueOf"]

Object.getOwnPropertyNames(Object.prototype);
//  ["toSource", "toString", "valueOf", "watch", "unwatch", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "__proto__",
//  "constructor", "toLocaleString", "__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__"]

Object.getOwnPropertyNames(Function.prototype);
//  ["toSource", "toString", "apply", "call", "isGenerator", "arguments", "caller", "constructor", "bind", "length", "name"]

Object.getOwnPropertyNames(Array.prototype);
//  ["length", "toSource", "toString", "toLocaleString", "join", "reverse", "sort", "push", "pop", "shift", "unshift","splice", "concat",
//  "slice", "lastIndexOf", "indexOf", "forEach", "map", "filter", "reduce","reduceRight", "some", "every", "find", "findIndex", "copyWithin",
//  "fill", "entries", "keys", "includes", "constructor"]

Object.getOwnPropertyNames(RegExp.prototype);
//  ["lastIndex", "toSource", "toString", "compile", "exec", "test", "flags", "global", "ignoreCase", "multiline", "source",
//  "sticky", "unicode", "constructor"]

Object.getOwnPropertyNames(Date.prototype);
//  ["getTime", "getTimezoneOffset", "getYear", "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth", "getDate", "getUTCDate",
//  "getDay", "getUTCDay", "getHours", "getUTCHours", "getMinutes", "getUTCMinutes", "getSeconds", "getUTCSeconds", "getMilliseconds",
//  "getUTCMilliseconds", "setTime", "setYear", "setFullYear", "setUTCFullYear", "setMonth", "setUTCMonth", "setDate", "setUTCDate",
//  "setHours", "setUTCHours", "setMinutes", "setUTCMinutes", "setSeconds", "setUTCSeconds", "setMilliseconds", "setUTCMilliseconds",
//  "toUTCString", "toLocaleFormat", "toLocaleString", "toLocaleDateString", "toLocaleTimeString", "toDateString", "toTimeString",
//  "toISOString", "toJSON", "toSource", "toString", "valueOf", "constructor", "toGMTString"]

Object.getOwnPropertyNames(Error.prototype);
//  ["fileName", "lineNumber", "columnNumber", "message", "name", "toSource", "toString", "stack", "constructor"]
K0II commented 8 years ago

九个内置对象(函数)自己的property

Object.getOwnPropertyNames(String);
//  ["prototype", "toLowerCase", "toUpperCase", "charAt", "charCodeAt", "includes", "contains", "indexOf", "lastIndexOf",
//   "startsWith", "endsWith", "trim", "trimLeft", "trimRight", "toLocaleLowerCase", "toLocaleUpperCase", "normalize",
//   "match", "search", "replace", "split", "concat", "fromCharCode", "fromCodePoint", "raw", "substring", "substr",
//   "slice", "localeCompare", "length", "name"]

Object.getOwnPropertyNames(Number);
//  ["prototype", "NaN", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "MAX_VALUE", "MIN_VALUE", "MAX_SAFE_INTEGER",
//   "MIN_SAFE_INTEGER", "EPSILON", "isFinite", "isInteger", "isNaN", "isSafeInteger", "parseInt", "parseFloat",
//   "length", "name"]

Object.getOwnPropertyNames(Boolean);
//  ["prototype", "length", "name"]

Object.getOwnPropertyNames(Object);
//  ["setPrototypeOf", "getOwnPropertyDescriptor", "keys", "is", "defineProperty", "defineProperties", "create",
//   "getOwnPropertyNames", "getOwnPropertySymbols", "preventExtensions", "freeze", "isFrozen", "seal", "isSealed",
//   "prototype", "assign", "getPrototypeOf", "isExtensible", "length", "name"]

Object.getOwnPropertyNames(Function);
//  ["prototype", "length", "name"]

Object.getOwnPropertyNames(Array);
//  ["join", "reverse", "sort", "push", "pop", "shift", "unshift", "splice", "concat", "slice", "isArray", "lastIndexOf",
//   "indexOf", "forEach", "map", "filter", "every", "some", "reduce", "reduceRight", "from", "of", "prototype", "length", "name"]

Object.getOwnPropertyNames(RegExp);
//  ["input", "multiline", "lastMatch", "lastParen", "leftContext", "rightContext", "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8",
//   "$9", "$_", "$*", "$&", "$+", "$`", "$'", "prototype", "length", "name"]

Object.getOwnPropertyNames(Date);
//  ["UTC", "parse", "now", "prototype", "length", "name"]

Object.getOwnPropertyNames(Error);
//  ["prototype", "length", "name"]
K0II commented 8 years ago

some tests

var a = [1,2,3];

Object.getOwnPropertyNames(a);    //  ["0", "1", "2", "length"]

'toString' in a;     //  true

'prototype' in a;    //  false

'constructor' in a;    //  true

a.constructor;      //  Array()

a.constructor.toString();
//  function Array() {
//      [native code]
//  }

toStringconstructor都是向上查找原型链时找到的属性

注:将a替换成new调用生成的数组对象结果是一样的