我正在学习js原型,我想知道下面两个部分之间是否有什么不同?
Segment1:
function SuperType(){
this.color=["blue","yellow"];
}
function SubType(){
}
Subtype.prototype = new SuperType();
Segment2
function SuperType(){
this.color=["blue","yellow"];
}
function SubType(){
SuperType
我在看中关于继承和原型的两个例子。鉴于这两个例子,我的理解似乎有些矛盾--它们似乎相互矛盾:
var a = {a: 1};
//inheritance looks like: a ---> Object.prototype ---> null
var b = Object.create(a);
//inheritance looks like: b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 (inherited)
到目前为止,这是有意义的,但是在另一个页面上,学习.call()方法
function dog(){
this.barking = "woof";
this.bark = function(){console.log(this.barking)}
}
var tony = new dog();
var stark = {};
dog.call(stark);
tony.bark();// "woof"
stark.bark();// "woof"
console.log(tony.prototype == stark.prototype);// true
console.log(to
为什么这里说的是“动物”而不是“猫”?
// create base class Animal
function Animal(animalType) {
this.type = animalType;
this.sayType = function () {
alert(this.type);
};
}
// create derived class Cat
function Cat(myName) {
Animal.call(this, "cat"); // cat calls the Animal base class c
我想编写一个函数来配置原型链,但我希望避免直接使用__proto__或Object.setPrototypeOf。这能办到吗?如果不是的话,你认为最好的方法是通过性能检查吗?
另外,将此描述为mixins的实现是否正确?
这就是我所拥有的(使用__proto__):
function mix(fn) {
var ancestorFns, curr
if (typeof fn !== 'function') {
throw 'fn must be a function.';
}
ancestorFns = Array.p
我正在研究jQuery源代码是如何工作的,我理解jQuery对象只是将一个调用转发给jQuery.fn.init,其中jQuery.fn只是对jQuery.prototype的引用。
然后在源代码中,有以下一行:
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
有一个注释可以解释代码的作用,但我仍然无法理解它。
有人能解释一下这一行代码是什么意思吗?接下来它在讨论什么实例化,为什么我们需要在jquery的原型中设置它的原型
既然doOtherStuff函数是直接在b实例上定义的,而不是在prototype链中“上面”定义的(比如base甚至Object),那么为什么b.hasOwnProperty('doOtherStuff')返回false呢?
var base = (function () {
var cls = function () { };
cls.prototype.doStuff = function () {
console.log('dostuff');
};
return cls;
})();
var c
我在javascript oop中遇到了一个node.js结构问题。我使用另一个类创建了一个类。
var base = function (name) {
this.Rows = new Array();
};
var top = function (name) {
this.name = name;
};
top.prototype = new base();
var myClass = new top("");
myClass.Rows.push("a");
console.log(myClass.Rows);
v