Javascript does not have the concept of class. It is a prototyal language which means objects inherits directly from other projects.
The book introduces several ways to implement the inheritance.
1. Pseudoclassical
2. Prototypal
3. Functional
4. Parts
Below, I give an example of Functional inheritance, which I think is the most "javascript way".
function parent(s) {
var name = s.name;
var o={};
o.getName = function(){
return name;
};
return o;
}
var p=parent({name:"zhangruimin"});
console.log(p.getName());
function child(s) {
var title = s.title; //private field
var o=parent(s); //like call the parent constructor
var getParentName=o.getName;// store to implement override
var getTitle= function(){// private function
return title;
}
o.getName = function(){
return "In child: "+ getTitle()+getParentName();
};
return o;
}
var c=child({name:"zhangruimin", title:"MR."});
console.log(c.getName());
Here, we've got:
1. inheritance
2. private function and field
3. Call parent method
分享到:
相关推荐
Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative ...
创建窗体并管理多个窗体程序的范例源代码<br>WeatherGage: 一个自定义基类窗体的范例源代码<br>Inheritance: 从一个自定义父类窗体继承创建窗体的范例源代码<br><br>第14章<br>DrawRectangles: 使用触笔绘制矩形...
Maven允许在POM中配置插件,通过`<build><plugins>`和`<profiles>`标签可以定制化构建过程,比如设置源代码编码、资源过滤等。 总结,Maven的多项目打包模块通过POM文件实现了项目的分层管理,简化了大型项目中的...
cout << "name: " << name << endl; cout << "sex: " << sex << endl; } }; class Student1 { int num; char name[20]; char sex; int age; char addr[20]; public: void display() { // 成员函数display...
ass31 Object is created with values: " << a << ", " << b << ", " << c << std::endl; } ~MyBase31() { std::cout << "…BaseClass31 Object is destroyed!" << std::endl; }};class MyDerived31 : public MyBase...
std::cout << "操作系统版本: " << osvi.dwMajorVersion << "." << osvi.dwMinorVersion << std::endl; } else { std::cout << "获取操作系统版本失败" << std::endl; } ``` 3. **显示当前内存中所有进程** ...
std::cout << "Rectangle area: " << shape2->Area() << ", perimeter: " << shape2->Perimeter() << std::endl; std::cout << "Triangle area: " << shape3->Area() << ", perimeter: " << shape3->Perimeter() ...
std::cout << max<int>(i, 3) << std::endl; // 正确:显式指定类型 ``` #### 异常处理 异常处理提供了一种在运行时检测错误并采取适当措施的机制,增强了程序的健壮性。例如: ```cpp try { if (x == 0) throw...
std::cout << "I can eat!" << std::endl; } }; class Dog : public Animal { public: void bark() { std::cout << "I can bark!" << std::endl; } }; ``` 在这个例子中,`Dog`类继承自`Animal`类,表示`Dog`...
JavaScript The Good Parts Chapter 1. Good Parts Chapter 2. Grammar Chapter 3. Objects Chapter 4. Functions Chapter 5. Inheritance Chapter 6. Arrays Chapter 7. Regular Expressions Chapter 8. Methods ...
cout << "三门成绩:" << score[0] << "\t" << score[1] << "\t" << score[2] << endl; cout << "总成绩和平均分:" << total << "\t" << ave << endl; } private: char stuno[20]; // 学号 float score[3], ...
std::cout << "Value: " << obj.getVar() << std::endl; return 0; } ``` ### 继承(Inheritance) 继承是面向对象编程中的一个重要特性,允许我们定义一个类继承另一个类的特性和行为。这意味着子类可以复用父类...
Part 1: The Basics -The Map Data Structure Part 1: The Basics -Type Synonyms Part 1: The Basics -Characters Part 1: The Basics -Character Categories Part 1: The Basics -Case-Folding Part 1: The Basics...
- `<dependencies>`:声明项目所需的外部依赖,包括版本信息。 - `<build>`:配置构建相关的设置,如源码编译、资源复制等。 6. Maven的命令行使用: - `mvn clean`:清除项目产生的临时文件和编译结果。 - `...
void display() { std::cout << "Age: " << age << std::endl; } }; ``` 在这个例子中,`MyClass`包含一个公开的数据成员`age`和一个公开的成员函数`display`。数据成员存储对象的状态,而成员函数描述对象的行为...
JavaScript,作为一种广泛应用于Web开发的脚本语言,其高级特性如闭包(closures)、原型(prototype)和继承(inheritance)是理解其精髓的关键。本文将深入探讨这些概念,帮助开发者更好地掌握JavaScript的核心。 ...
Zakas thoroughly explores JavaScript's object-oriented nature, revealing the language's unique implementation of inheritance and other key characteristics. You'll learn: –The difference between ...