我有以下代码:
function Rune(){
this.subSpells = [];
}
function Modifier(){
this.type = "modifier";
}
Modifier.prototype = new Rune();
function RuneFactory(effect, inheritsFrom, initialValue){
var toReturn = function(){};
toReturn.prototype = new inheritsFrom();
toReturn.prototype.subSpells[effect] = initialValue;
return toReturn;
}
Duration = RuneFactory("duration", Modifier, 1);
Quicken = RuneFactory("quicken", Modifier, 1);
x = new Duration();
y = new Quicken();
Run Code Online (Sandbox Code Playgroud)
x.subSpells.duration和x.subSpells.quicken都等于1.与y相同.我希望x.subSpells.quicken和y.subSpells.duration未定义.
如果我为Duration和Quicken定义执行以下操作,我会得到我想要的行为.
Duration = RuneFactory("duration", Rune, 1);
Quicken = RuneFactory("quicken", Rune, 1);
Run Code Online (Sandbox Code Playgroud)
我认为双重继承存在问题.任何人都可以告诉我如何更改我的RuneFactory代码,使其适用于双重继承和/或解释什么是破坏?如果可能的话,我想避免使用框架.
谢谢!
var x = {
name: "japan",
age: 20
}
x.prototype.mad = function() {
alert("USA");
};
x.mad();
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用。对象字面量不能扩展?或x.mad()呼叫方式不正确。
我正在努力学习和提高我的javascript技能.
一个非常有用的工具是firebug,我可以用它来检查不同的javascript对象.
但是,我有一些问题:
一些对象名称(如jQuery,$,fn等)以红色显示.为什么?
其他一些对象有一个"原型"属性,不是粗体.那是什么以及何时使用/实施它是好还是不好?
大多数功能显示为"function()".但是也有一些其他功能被显示,例如,"u(M)","z()","B(E)".为什么他们不同?

谢谢
下面的代码将记录a,a.length和b.test.a和b.test都产生[1,2,3].编辑 - 我搞砸了.b.test产生undefined.请参阅下面的raina的回复.
a.length产生3.
b.test.length失败并显示"无法读取未定义的属性'长度'"
当a和b.test相等时,为什么会这样?
var a = [1,2,3];
var b = function(){};
b.prototype.test=[1,2,3];
console.log(a);
console.log(a.length);
console.log(b.test);
console.log(b.test.length);
Run Code Online (Sandbox Code Playgroud) 这两个代码有什么区别,我应该使用哪一个?
function Test() {}
Test.method = function() {};
Run Code Online (Sandbox Code Playgroud)
使用Prototype:
function Test() {}
Test.prototype.method = function() {};
Run Code Online (Sandbox Code Playgroud) 为什么是原型property的instance投掷undefined?。
function Test(name){
this.name = name;
}
Test.prototype = {
constructor: Test,
sayHello : function(){
console.log(this.name);
}
}
var z = new Test("Hello");
console.log(z.prototype); //undefined
console.log(zUtils.constructor); // Test
Run Code Online (Sandbox Code Playgroud)
我可以通过z.sayHello()访问 sayHello 方法,那么为什么我instance prototype返回的我是 undefined 而不是Test.prototype?。
我在StackOverflow上找到了这段代码:
[].sort.call(data, function (a, b) {})
Run Code Online (Sandbox Code Playgroud)
是[]"对数据值进行排序然后创建具有相同名称的数组"的简写吗?
https://github.com/lydiahallie/javascript-questions#14-all-object-have-prototypes 除基础对象外,所有对象都有原型。什么是基础对象
假设我有以下代码;
var A = {a:10};
var B = {b:20};
B.prototype = A;
alert(B.a);
Run Code Online (Sandbox Code Playgroud)
我对Ba的定义不明确.难道我做错了什么?如何设置对象文字的原型?
我知道如何为Constructor对象做.所以下面的代码是完美的
function A(){this.a=10}
function B(){this.b=20}
B.prototype = new A();
b = new B;
alert(b.a);
Run Code Online (Sandbox Code Playgroud)
我如何为对象文字做到这一点?