Object Oriented Programming继承

基于原型的继承
例:
function Foo(){
this.y=2;
}
console.log(typeof Foo.prototype); //"Object"
Foo.prototype.x=1;
var obj3=new Foo();

console.log(obj3.y); //2
console.log(obj3.x); //1

图解:
注意:Object.prototype属性与原型proto是两个概念





prototype属性与原型
例:
function Foo(){}
typeof Foo.prototype; //"object" prototype是函数对象上预设的对象属性,原型是对象的原型,原型通常都是构造器上的(new Foo())上的prototype(对象的)属性,如图解2
Foo.prototype.x=1;
var obj3=new Foo();

图解1:
图解2:
Foo.prototype属性所包含的属性,这里的_proto_属性值为Object.prototype,因此Object.prototype里面的方法,比如说toString、valueOf等方法才会被一般的每一个方法所使用







继承实例
function Person(name,age){
this.name=name;
this.age=age;
}

Person.prototype.hi=function(){
console.log("Hi,my name is"+this.name+",I'm"+this.age+"years old now.");
};

Person.prototype.LEGS_NUM=2;
Person.prototype.ARMS_NUM=2;
Person.prototype.walk=function(){
console.log(this.name+"is walking...");
}

function Student(name,age,className){
this.name=name;
this.age=age;
this.className=className;
}

Student.prototype=Object.create(Person.prototype);
// 为何不写为:Student.prototype=Person.prototype ?因为若是这样子写,那么Student与Person就会指向同一个对象,当我们给Student.prototype增加属性时,同时也给Person.prototype增加同样的属性,所以我们用Object.create方法,这样我们可以向上访问到Person对象,也可以在不影响Person.prototype的前提下创建Student.prototype属性
Student.prototype.constructor=Student;

Student.prototype.hi=function(){
console.log('Hi,my name is'+this.name+',I am'+this.age+"years old now,and from "+this.className+".");
};
Student.prototype.learn=function(subject){
console.log(this.name+'is learning'+subject+'at'+this.className+'.');
};


var bosn=new Student('Bosn',27,'Class 3,Grade 2');
bosn.hi();
console.log(bosn.LEGS_NUM);
bosn.walk();
bosn.learn('math');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title: R Object-Oriented Programming Author: Kelly Black Length: 190 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2014-10-23 ISBN-10: 1783986689 ISBN-13: 9781783986682 A practical guide to help you learn and understand the programming techniques necessary to exploit the full power of R About This Book Learn and understand the programming techniques necessary to solve specific problems and speed up development processes for statistical models and applications Explore the fundamentals of building objects and how they program individual aspects of larger data designs Step-by-step guide to understand how OOP can be applied to application and data models within R Who This Book Is For This book is designed for people with some experience in basic programming practices. It is also assumed that they have some basic experience using R and are familiar using the command line in an R environment. Our primary goal is to raise a beginner to a more advanced level to make him/her more comfortable creating programs and extending R to solve common problems. In Detail R is best suited to produce data and visual analytics through customizable scripts and commands, instead of typical statistical tools that provide tick boxes and drop-down menus for users. The book is divided into three parts to help you perform these steps. It starts by providing you with an overview of the basic data types, data structures, and tools available in R that are used to solve common tasks. It then moves on to offer insights and examples on object-oriented programming with R; this includes an introduction to the basic control structures available in R with examples. It also includes details on how to implement S3 and S4 classes. Finally, the book provides three detailed examples that demonstrate how to bring all of these ideas together. Table of Contents Chapter 1. Data Types Chapter 2. Organizing Data Chapter 3. Saving Data and Printing Results Chapter 4. Calculating Probabilities and Random Numbers Chapter 5. Character and String Operations Chapter 6. Converting and Defining Time Variables Chapter 7. Basic Programming Chapter 8. S3 Classes Chapter 9. S4 Classes Chapter 10. Case Study – Course Grades Chapter 11. Case Study – Simulation
Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust apps Implement Object-Oriented concepts in a wide range of frontend architectures Capture objects from real-world elements and create object-oriented code that represents them Learn the latest ES6 features and how to test and debug issues with JavaScript code using various modern mechanisms Who This Book Is For JavaScript developers looking to enhance their web developments skills by learning object-oriented programming. What You Will Learn Get acquainted with the basics of JavaScript language constructs along with object-oriented programming and its application. Learn to build scalable server application in JavaScript using Node.js Generate instances in three programming languages: Python, JavaScript, and C# Work with a combination of access modifiers, prefixes, properties, fields, attributes, and local variables to encapsulate and hide data Master DOM manipulation, cross-browser strategies, and ES6 Identify and apply the most common design patterns such as Singleton, Factory, Observer, Model-View-Controller, and Mediator Patterns Design applications using a modular architecture based on SOLID principles In Detail JavaScript is the behavior, the third pillar in today's paradigm that looks at web pages as something that consists of : content (HTML), presentation (CSS), and behavior (JavaScript). Using JavaScript, you can create interactive web pages along with desktop widgets, browser, and application extensions, and other pieces of software. Object-oriented programming, which is popularly known as OOP, is basically based on the concept of objects rather than actions. The first module will help you master JavaScript and build futuristic web applications. You will start by getting acquainted with the language constructs and how to organize code easily. You develop conc

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值