3 回答
TA贡献1995条经验 获得超2个赞
function Class () {}Class.prototype.calc = function (a, b) { return a + b;}// Create 2 instances:var ins1 = new Class(), ins2 = new Class();// Test the calc method:console.log(ins1.calc(1,1), ins2.calc(1,1));// -> 2, 2 // Change the prototype methodClass.prototype.calc = function () { var args = Array.prototype.slice.apply(arguments), res = 0, c; while (c = args.shift()) res += c; return res; }// Test the calc method:console.log(ins1.calc(1,1,1), ins2.calc(1,1,1));// -> 3, 3
ins1
ins2
calc()
ins1
ins2
calc()
function Class() {}
var Class = function () {}
// Error, fn is called before the function is assigned!fn();var fn = function () { alert("test!"); } // Works as expected: the fn2 declaration is hoisted above the callfn2();function fn2() { alert("test!"); }
TA贡献1786条经验 获得超13个赞
calc()
Class
Class
Class
Class
calc()
function Class() { var calcCallCount = 0; this.calc = function (a, b) { ++calcCallCount; alert("Calc called " + calcCallCount + " times"); return a + b; };};
var Class = function() {...}
function Class() {...}
添加回答
举报