撸完第十章,作个记录

感觉慢慢来了,看来一切都需要编码的累积。。。

 

Person = function() {
  var name = "default";
  return {
    getName: function() {
      return name;
    },
    setName: function(newName) {
      name = newName;
    }
  };
};

var john = Person();
console.log(john.getName());
john.setName("john");
console.log(john.getName());

var jack = Person();
console.log(jack.getName());
jack.setName("jack");
console.log(jack.getName());


var person = function() {
  var name = "default";
  return {
    getName: function() {
      return name;
    },
    setName: function(newName) {
      name = newName;
    }
  };
}();

console.log(person.name);
console.log(person.getName());
console.log(person.setName("abruzzi"));
console.log(person.getName());


function Base() {
  this.baseFunc = function() {
    console.log("base behavior");
  };
}

function Middle() {
  this.middleFunc = function() {
    console.log("middle behavior");
  };
}

Middle.prototype = new Base();

function Final() {
  this.finalFunc = function() {
    console.log("final behavior");
  };
}

Final.prototype = new Middle();

function test() {
  var obj = new Final();
  obj.baseFunc();
  obj.middleFunc();
  obj.finalFunc();
  
}
test();


var obj = {};
var ref = obj;

obj.name = "objectA";
console.log(ref.name);

obj = ["one", "two", "three"];
console.log(ref.name);
console.log(obj.length);
console.log(ref.length);

var obj = {};
var ref1 = obj;
var ref2 = obj;

obj.func = "function";

console.log(ref1.func);
console.log(ref2.func);

function Shape(type) {
  this.type = type || "rect";
  this.calc = function() {
    return "calc, " + this.type;
  };
}

var triangle = new Shape("triangle");
var circle = new Shape("circle");
console.log(triangle.calc());
console.log(circle.calc());

function Person(name) {
  var address = "The Earth";
  this.getAddress = function() {
    return address;
  };
  this.name = name;
}

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

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

Person.TAG = "Javascript TAG";

var jack = new Person("jack");
console.log(jack.name);
console.log(jack.getName());
console.log(jack.address);
console.log(jack.getAddress());
console.log(Person.TAG);


var adder = function(num) {
  return function(y) {
    return num + y;
  };
};

var inc = adder(1);
var dec = adder(-1);

console.log(inc(99));
console.log(dec(99));
console.log(adder(100)(99));

console.log(adder(100)(-99));

var base = {
  name: "base",
  getInfo: function() {
    return this.id + ":" + this.name;
  },
};

var ext1 = {
  id: 0,
  name: "ext1",
  __proto__: base,
};


var ext2 = {
  id: 9,
  name: "ext2",
  __proto__: base,
};

console.log(ext1.id);
console.log(ext1.getInfo());
console.log(ext2.id);
console.log(ext2.getInfo());


function Task(id) {
  this.id = id;
}

Task.prototype.status = "STOPPED";
Task.prototype.execute = function(args) {
  return "execute task_" + this.id + "[" + this.status + "]:" + args;
};

var task1 = new Task(1);
var task2 = new Task(2);

console.log(task1.execute("task1"));
console.log(task2.execute("task2"));

(function(name){
  console.log("hello, " + name);
})("jack");

function func(handle, message) {
  var id = 1;
  function doNothing(x) {
    return x;
  };
  handle(message);
};

func(console.log, "hello");

var topone = "top-level";

(function outter(){
  var middle = "middle-level";
  (function inner(){
    var bottom = "bot-level";
    console.log(topone + ">" + middle + ">" + bottom);
  })();
})();

var attribute = "attribute";

console.log(attribute);
console.log(this.attribute);

var global = this;
var tom = {
  name: "Tome",
  home: "desine",
  getInfo: function(){
    console.log(this.name + ", from " + this.home);
  },
};

tom.getInfo();

var jerry = {
  name: "jerry",
  getInfo: tom.getInfo,
};

jerry.getInfo();

global.getInfo = tom.getInfo;
global.getInfo();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值