Javascript继承问题

发布于 2024-11-04 17:07:16 字数 508 浏览 0 评论 0原文

为什么下面代码中的版本 2 不会产生与版本 1 相同的结果?

function person(name) {
    this.name = name;
}
function student(id, name) {
    this.id = id;
    // Version 1
    //this.inherit_from_person = person;
    //this.inherit_from_person(name);
    // Version 2
    person(name);
}
s = new student(5, 'Misha');
document.write(s.name); // Version 1    =>    Misha
                        // Version 2    =>    undefined

现场演示。

Why Version 2 in the code below does not produce the same result as Version 1 ?

function person(name) {
    this.name = name;
}
function student(id, name) {
    this.id = id;
    // Version 1
    //this.inherit_from_person = person;
    //this.inherit_from_person(name);
    // Version 2
    person(name);
}
s = new student(5, 'Misha');
document.write(s.name); // Version 1    =>    Misha
                        // Version 2    =>    undefined

Live demo here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-11-11 17:07:17

当您调用 person(name) 时,它会通过绑定到全局对象的 this 进行调用,因此只需设置 window.name = "Misha" 。您希望 person.call(this, name) 将其显式绑定到右侧的 this

When you call person(name) it gets called with this bound to the global object, so that's just setting window.name = "Misha". You want person.call(this, name) to explicitly bind it to the right this.

情话已封尘 2024-11-11 17:07:17

在我看来,您正在尝试实现原型继承。下面是一个经典的例子,虽然用得不多。 javascript 中不需要复杂的继承,通常只需要单个实例即可。如果需要多个实例,模块模式可以与共享方法和属性的闭包一起使用,也可以提供私有和特权成员。

// Person is the "base class"
function Person(name) {
  this.setName(name);
}

// Use setters and getters so properties are
// added appropriately.
Person.prototype.setName = function(name) {
  this.name = name;
}

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

// Student inherits from Person and also has
// its own methods
function Student(name, id) {
  this.setId(id);
  this.setName(name);
}

// To inherit from Person, Student.prototype should
// be an instance of Person
Student.prototype = new Person();

// Add Student methods
Student.prototype.setId = function(id) {
  this.id = id;
}
Student.prototype.getId = function() {
  return this.id;
}

var p0 = new Student('Sally', '1234');
var p1 = new Person('James');

alert('p0\'s id is ' + p0.id + ' and name is: ' + p0.name);
alert('p1\'s name is: ' + p1.name);
alert('Is p0 a student? ' + (p0 instanceof Student));
alert('Is p1 a student? ' + (p1 instanceof Student));

请注意,instanceof 运算符不是很可靠,但在上述情况下它可以正常工作。此外,所有方法和属性都是公共的,因此很容易被重写。

It looks to me like you are trying to implement prototype inheritance. Below is a classic example, though not much used. Complex inheritance is just not needed in javascript, usually a single instance is all that is required. If multiple instances are required, the module pattern can be used with closures for shared methods and properties and also to provide private and priveliged members.

// Person is the "base class"
function Person(name) {
  this.setName(name);
}

// Use setters and getters so properties are
// added appropriately.
Person.prototype.setName = function(name) {
  this.name = name;
}

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

// Student inherits from Person and also has
// its own methods
function Student(name, id) {
  this.setId(id);
  this.setName(name);
}

// To inherit from Person, Student.prototype should
// be an instance of Person
Student.prototype = new Person();

// Add Student methods
Student.prototype.setId = function(id) {
  this.id = id;
}
Student.prototype.getId = function() {
  return this.id;
}

var p0 = new Student('Sally', '1234');
var p1 = new Person('James');

alert('p0\'s id is ' + p0.id + ' and name is: ' + p0.name);
alert('p1\'s name is: ' + p1.name);
alert('Is p0 a student? ' + (p0 instanceof Student));
alert('Is p1 a student? ' + (p1 instanceof Student));

Note that the instanceof operator is not very reliable, but it works fine in the above case. Also all methods and properties are public so easily over written.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文