标签:
var a = {"name": "Hello"}; var b = a; a.name; // "Hello" b.name = "World"; a.name; // "World"
var reg = /\d+/g; //等价于 var reg = new RegExp("\\d+", "g");
var name = "Nicholas"; name.last = "Zakas"; console.log(name.last); // undefined
这段代码实际的执行过程如下:
var name = "Nicholas"; var temp = new String(name); temp.last = "Zakas"; temp = null; var temp = new String(name); console.log(temp.last); // undefined temp = null;
所以在获取时,读取的都是临时变量,用后即焚。
function showMessage(message) { console.log("Parameter: " + message); } function showMessage() { console.log("No Parameter"); } showMessage("Hello"); // "No Parameter"
利用原始封装类型的概念帮助理解执行过程如下:
var showMessage = new Function("message", "console.log(\"Parameter: \" + message)"); showMessage = new Function("console.log(\"No Parameter\")"); showMessage("Hello"); // "No Parameter"
重载的本质在于参数列表的不同,可以通过判断参数的个数、类型等执行不同的处理过程,实现重载。
function Rectangle(length, width) { this.length = length; this.width = width; } Rectangle.prototype.getArea = function() { return this.length * this.width; } Rectangle.prototype.toString = function() { return "[Rectangle " + this.length + " x " + this.width + "]"; } function Square(size) { this.length = size; this.width = size; } Square.prototype = Object.create(Rectangle.prototype, { constructor: { configurable: true, enumerable: true, value: Square, writable: true } }; Square.prototype.toString = function() { return "[Square " + this.length + " x " + this.width + "]"; } var rect = new Rectangle(5, 10); var square = new Square(6); console.log(rect.getArea()); // 50 console.log(square.getArea()); // 36 console.log(rect.toString()); // "[Rectangle 5x10]" console.log(square.toString()); // "[Square 6x6]" console.log(rect instanceof Rectangle); // true console.log(rect instanceof Object); // true console.log(square instanceof Square); // true console.log(square instanceof Rectangle); // true console.log(square instanceof Object); // true
var person = (function(){ var age = 25; return { name: "Nicholas", getAge: function() { return age; }, growOlder: function() { age++; } }; }()); console.log(person.name); // "Nicholas" console.log(person.getAge()); // 25 person.age = 100; console.log(person.getAge()); // 25 person.growOlder(); console.log(person.getAge()); // 26
function Person(name){ var age = 25; this.name = name; this.getAge = function() { return age; }; this.growOlder = function() { age++; }; } var person = new Person("Nicholas"); console.log(person.name); // "Nicholas" console.log(person.getAge()); // 25 person.age = 100; console.log(person.getAge()); // 25 person.growOlder(); console.log(person.getAge()); // 26
var Person = (function(){ var age = 25; function realPerson(name) { this.name = name; } realPerson.prototype.getAge = function() { return age; } realPerson.prototype.growOlder = function() { age++; } return realPerson; }()); var Nicholas = new Person("Nicholas"); var Greg = new Person("Greg"); console.log(Nicholas.name); // "Nicholas" console.log(Nicholas.getAge()); // 25 console.log(Greg.name); // "Nicholas" console.log(Greg.getAge()); // 25 Nicholas.growOlder(); console.log(Nicholas.getAge()); // 26 console.log(Greg.getAge()); // 26
这个问题的出现来自于创建对象实例时,很容易漏写new关键字。构造函数也是函数,所以可以不用new操作符直接调用它们来改变this的值。在非严格模式下,this被强制指向全局对象;在严格模式下,构造函数抛出错误。解决这个问题,可在构造函数中进行检查。
function Person(name) { if(this instanceof Person) { this.name = name; } else { return new Person(name); } }
标签:
原文地址:http://www.cnblogs.com/SheldonWr/p/5399327.html