您的位置:首页 > Web前端 > JavaScript

javascript笔记

2016-04-30 13:43 597 查看
存取器属性:

var o={$n:0,

get n(){return this.$n;},

set n(n){this.$n=n;}

};

o.n=5;

o.n //5;

o.hasOwnProperty("$n"); //true;

o.propertyIsEnumerable("toString"); //false;
Object.getOwnPropertyDescriptor(o,"$n");

Object.defineProperty(o,"$n",{value:7,writable:false,enumerable:true})

Object.prototype.toString.call(p);//判断类型

p instanceof Point;

p.constructor===Point;

typeof p;//will return Object.

Object.preventExtention(o);
Object.isExtensible(o);

function fun(){
arguments.length;实参个数
this.length;//形参个数
}
function Point(x,y){this.x=x;this.y=y;}
Point.prototype.show=function(){console.log(x+y);}
var p=new Point(3,4);
p.constructor===Point //true;
----------------------------------
function Point(x, y) {

  this.x = x;

  this.y = y;

}

Point.prototype = {

  constructor:Point,

  show: function () {

    console.log(x + y);

  }

}

var p = new Point(3, 4);

p.constructor === Point //true;

---------------------------------------

javascript: function enumeration(nameToValues) {

  var enumeration = function () {

    throw 'can\'t instantiate the instance!'

  };

  var enumerator = function () {

  };

  var proto = enumerator.prototype = {

    constructor: enumeration,

    toString: function () {

      return this.name;

    },

    valueOf: function () {

      return this.value;

    },

    toJson: function () {

      return this.name;

    }

  };

  enumeration.values = [

  ];

  for (var name in nameToValues) {

    var e = new enumerator;

    e.name = name;

    e.value = nameToValues[name];

    enumeration[name] = e;

    enumeration.values.push(e);

  }

  return enumeration;

}

var Cards = enumeration({

  Red: 1,

  Green: 2,

  Blue: 3

});

Cards['Red']
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: