Three methods of detecting objects are as follows: instanceof, constructor, and constructor name: 1) instanceofconsole. log ([1, 2, 3] instanceofArray); trueconsole. log ([, 3] instanceofObject); true although the constructor is prototype only... three methods of detecting objects are as follows: instanceof, constructor, and constructor name usage: 1) instanceof console. log ([1, 2, 3] instanceof Array); true console. log ([1, 2, 3] instanceof Object); true although the constructor is the unique identifier of the prototype, the right operand of the instanceof operator is the constructor, in the actual computing process, instanceof detects the inheritance relationship of objects, rather than the constructor used to create objects. It only uses constructor as the intermediary when You can also use isPrototypeOf to determine whether an object exists in the prototype chain of another object. In this case, the constructor var a1 = new Array (); console is not used. log (Array. prototype. isPrototypeOf (a1); true console. log (Array. prototype. isPrototypeOf ([, 3]); true Note: When multiple execution contexts exist (for example, when different frameworks exist) instanceof has a limit of 2) constructor each javascript function can be used as a constructor. To call a constructor, you must use the prototype attribute. Therefore, each javascript function automatically has the prototype attribute, which is an object, this object contains a contructor attribute. The constructor attribute value is a function object. That is, for the function var F = function () {}; F. prototype. constructor = F: Example: copy the code var F = function () {}; var p = F. prototype; var c = p. constructor; console. log (p); console. log (c); console. log (c = F); Object {} function () {} true copies the code. Therefore, the constructor inherited by the Object refers to their constructor eg: var o = new F (); console. log (o. constructor = F); // Output true var a = new Array (); console. log (. constructor === Array); // output the true copy code function typeDiscern (x) {switc H (x. constructor) {case Number: return "Number:" + x; case String: return "String:" + x; case Array: return "Array:" + x ;}} console. log (typeDiscern ([1, 2, 3]); console. log (typeDiscern ("abc"); console. log (typeDiscern (5); // output Array: 1, 2, 3 String: abcNumber: 5 copy the code. Note: instanceof cannot be used in multiple contexts, in addition, not all objects contain the constructor attribute eg: defines the Person class copy code function Person (name) {this. name = name; this. getName = function () {return this. n Ame ;}}; var wish = new Person ('js'); console. log (wish. constructor = Person); console. log (Person. prototype); console. log (Person. constructor); console. log (wish. getName (); // output truePerson {} function Function () {[native code]} js copy code to Person custom prototype copy code function Person (name) {this. name = name; this. getName = function () {return this. name ;}}; Person. prototype = {toString: function () {return this. name; }; Var wish = new Person ('js'); console. log (wish. constructor = Person); console. log (Person. prototype); console. log (Person. constructor); console. log (wish. getName (); console. log (wish. toString (); // outputs falseObject {toString: function} function Function () {[native code]} jsjs to copy the code. The new prototype object does not include the constructor attribute, therefore, the instance of Person does not contain the constructor attribute. Solution: You can add the constructor to the prototype displayed. prototype = {constructor = Person, toString: fun Ction () {return this. name ;}}; the constructor name has no execution context issues with intanceof and constructor. The Array constructor in one window does not have the same name as the Array constructor in another window, but not every Function has a name to copy the code Function. prototype. getName = function () {if ("name" in this) {return this. name;} return this. name = this. toString (). match (/function \ s * ([^ (] *)/);} function test1 () {} console. log (test1.getName (); // output: test1, copy the code, duck-type authentication, and follow what the object can do, not the object class. James Whitcomb Riley proposed to walk, swim, and scream like a duck. The bird is the duck's main object, including the walk (), swim (), and bike () methods. These three methods can be used as parameters to pass in functions implemented using duck-style arguments: copy the code function quackImplements (o /*,... */) {for (var I = 1; I var arg = arguments [I]; switch (typeof arg) {case 'string': if (typeof o [arg]! = "Function") return false; continue; case 'function': arg = arg. prototype; case 'object': for (var m in arg) {if (typeof arg [m]! = "Function") continue; if (typeof o [m]! = "Function") return false ;}} return true ;} copy the code to directly check the naming method of the string. check whether there is a method of the same name for the object. check whether there are the same methods in the prototype object of the constructor. In javascript, many functions do not perform type detection on the object. what can these objects do? eg: the prototype of Array uses duck-like identification. arguments is a pseudo Array copy code (function () {var arr = Array. prototype. slice. apply (arguments); console. log (arr) ;}) (1, 2, 3); // output: [1, 2, 3] copy the code var arr = Array. prototype. slice. apply ({0: 1, 1: 2, 2: 3, length: 3}); console. log (arr); // output: [1, 2, 3] the use of duck-like pattern can expand the scope of use of the object. For example: let a common object have an array of push methods to copy the code Function. prototype. unCurrying = function () {var f = this; return function () {var a = arguments; return f. apply (a [0], []. slice. call (a, 1) ;};}; Function. prototype. unCurrying = function () {return this. call. bind (this) ;}; var push = Array. prototype. push. unCurrying (), obj = {}; push (obj, 'first', 'two'); console. log (obj); console. log ("length:" + obj. length) copy the code output: Object {0: "first", 1: "two", length: 2} length: 2
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service