推荐牛X的一本JS书

主要是看阮一峰的教程时,他参考书目里有这一本中文的,

找来一看,果然高。。

练习一下。

推荐牛X的一本JS书

function Base(name) {
  this.name = name;
  this.getName = function() {
    return this.name;
  };
}

function Child(id) {
  this.id = id;
  this.getId = function() {
    return this.id;
  };
}
Child.prototype = new Base("base");
var c1 = new Child("child");
console.log(c1.getId(), c1.getName());

function Person(name, age) {
  this.name = name;
  this.age = age;

  this.getName = function() {
    return this.name;
  };

  this.getAge = function() {
    return this.age;
  };

  this.getNameAge = function() {
    return this.name + this.age;
  };
}

var tom = new Person("Tom", 38);
var jerry = new Person("Jerry", 6);
console.log(tom.getName());
console.log(jerry.getAge());

var jack = {
  name: "jack",
  age: 26,
};

var abruzzi = {
  name: "abruzzi",
  age: 26,
};

function printName() {
  return this.name;
}

console.log(printName.call(jack));
console.log(printName.call(abruzzi));

function Address(street, xno) {
  this.street = street || "HuangQuan Road";
  this.xno = xno || 135;
  this.toString = function() {
    return "street : " + this.street + ", No : " + this.xno;
  };
}

function Person(name, age, addr) {
  this.name = name || 'unknown';
  this.age = age || 34;
  this.addr = addr || new Address(null, null);
  this.getName = function() {return this.name;};
  this.getAge = function() {return this.age;};
  this.getAddr = function() {return this.addr.toString();};
}

var jack = new Person('jack', 26, new Address("QingHai Road", 135));
var abruzzi = new Person('abruzzi', 26);

console.log(jack.getName());
console.log(jack.getAge());
console.log(jack.getAddr());
console.log(abruzzi.getName());
console.log(abruzzi.getAge());
console.log(abruzzi.getAddr());

function p() {
  console.log("invoke p by ()");
}

p.id = "func";
p.type = "function";

console.log(p);
console.log(p.id + ":" + p.type);
console.log(p());

function adPrint(str, len, option) {
  var s = str || "default";
  var l = len || s.length;
  var o = option || "i";

  s = s.substring(0, l);
  switch(o) {
    case "u":
      s = s.toUpperCase();
      break;
    case "l":
      s =s.toLowerCase();
      break;
    default:
      break;
  }
  console.log(s);
}

console.log(adPrint("Hello, world"));
console.log(adPrint("Hello, world", 5));
console.log(adPrint("Hello, world", 5, "l"));
console.log(adPrint("Hello, world", 5, "u"));

var jack = {
  name: "jack",
  age: 26,
};

var abruzzi = {
  name: "abruzzi",
  age: 27,
};

function printName() {
  return this.name + ":" + this.age;
}

function setName(name) {
  this.name = name;
}

setName.apply(jack, ["Jack Sept."]);
console.log(printName.apply(jack));
setName.call(jack,"Jack Sept.t");
console.log(printName.apply(jack));

function add(x, y) {
  return x + y;
}

var a = 0;
a = add;
var b = a(2, 3);
console.log(b);

var obj = {
  id: "obj1",
};

obj.func = add;
console.log(obj.func(2, 3));

function adPrint2(str, handler) {
  console.log(handler(str));
}
function up(str) {
  return str.toUpperCase();
}
function low(str) {
  return str.toLowerCase();
}

console.log(adPrint2("Hello,world", up));
console.log(adPrint2("Hello,world", low));

function currying() {
  return function() {
    console.log("curring");
  };
}
currying();
currying()();

var array = [1, 2, 3, 4, 5];
console.log(array.length);
array.length = 3;
console.log(array.length);
console.log(array);
var stack = new Array();
stack['second'] = "okay, then";
stack['first'] = 3.1415;
stack['third'] = new Date();

for(var item in stack) {
  console.log(typeof stack[item]);
}
console.log(stack.length);

var array = [];
array.push(1);
array.push(2);
array.push(3);
array.push("four");
array.push("five");
array.push(3.14159);

var len = array.length;
for(var i = 0; i < len; i++) {
  console.log(typeof array[i]);
}
for (var i = 0; i < len; i++) {
  console.log(array.pop());
}
console.log(array.length);
array = ["one", "two", "three", "four", "five"];
var str1 = array.join(",");
var str2 = array.join("|");
console.log(str1);
console.log(str2);
var another = ["this", "is", "another", "array"];
var another2 = ["yet", "is", "another", "array2"];
var bigArray = array.concat(another, another2);
console.log(bigArray);
console.log(bigArray.slice(5, 9));
console.log(bigArray.splice(5, 2));

var emailval = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
console.log(emailval.test("kmustlinux@hotmail.com"));
console.log(emailval.test("@hotmail.com"));
var variable = /[a-zA-Z_][a-zA-Z0-9_]*/;
console.log(variable.test("hello"));
console.log(variable.test("0871"));
var pid = /^(\d{15}|\d{18})$/;
var mphone = /\d{11}/;
var phone = /\d{3, 4}-\d{7, 8}/;
mphone.test("13865732134");
var str = "life is very much like a mirror.";
var result = str.match(/is|a/g);
console.log(result);
var str = "<span>Welcome, John</span>";
var result = str.replace(/span/g, "div");
console.log(result);
var result = str.replace(/(\w+), \s(\w+)/g, "$2 $1");
console.log(result);
var str = "john: wow : tomorrow: happy";
var result = str.split(/s*:\s*/);
console.log(result);
var str = "Tomorrow is another day";
var index = str.search(/another/);
console.log(index);

var outter = [];
function closeTest() {
  var array = ["one", "two", "three", "four", "five"];
  for(var i = 0; i < array.length; i++) {
    var x = {};
    x.no = i;
    x.text = array[i];
    x.invoke = function(no) {
      return function() {
         console.log(no);
      };
    }(i);
    outter.push(x);
  }
}
closeTest();
console.log(outter[0].invoke());
console.log(outter[1].invoke());
console.log(outter[2].invoke());
console.log(outter[3].invoke());
console.log(outter[4].invoke());

推荐牛X的一本JS书

上一篇:Django html标签make_safe


下一篇:Ajax 的 GET 和 POST 模式