javascript笔记

来源:互联网 发布:人工智能计算器 编辑:程序博客网 时间:2024/05/29 10:55

存取器属性:

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']


0 0
原创粉丝点击