201507010852_ "JavaScript authoritative Guide (sixth Edition)-the type function for judging value types, duck-type argument" (p210-217)

Source: Internet
Author: User

I. Expansion of the class

1. As long as you expand the prototype class, you can dynamically update to the instance. For example: xxx.prototype.do = function () {//...};

2. It is not recommended to use contaminated JavaScript methods , such as: Object.prototype.methods = function () {//...}; Use the methods in the authoritative guide 9.8.1 to add attributes, such as: Object.defineproperty (//...);


Two. Classes and types

1. isprototypeof ();


Three. Constructor properties

1. type function for judging value types

For example:

function type (obj) {
var t,c,n;
Handling null is worth the case
if (obj = = = null) return "NULL";
Nan and the two situations that are different from yourself
if (obj!== obj) return "NaN";
Not an object, just use this value
if (t = typeof (obj)!== "Object") return t;
Returns the class name of an object, unless it is a
if (c = classof (obj)!== "Object") return C;
If the object constructor name exists, it is returned
if (obj.constructor && typeof obj.constructor = = = "function" && (n = obj.constructor.getName ())) return n;
return "Object";
}; //For Classof (), getName () need to be defined

Four. Duck-type argument

function quacks (obj) {
for (var i = 1; i < arguments.length; i++) {
var arg = arguments[i];
Switch (typeof Arg) {
Case ' string ':
if (typeof Obj[arg]!== "function") return false;
Case ' function ':
arg = Arg.prototype;
Case ' object ':
For (var m in Arg) {
if (typeof Arg[m]!== "function") continue;
if (typeof Obj[m]!== "function") return false;
return true;

var f = function fn (x) {return x*x};  Alert (quacks (f)); //True

201507010852_ "JavaScript authoritative Guide (sixth Edition)-the type function for judging value types, duck-type argument" (p210-217)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.