由于constructor.prototype指向的是一object实例,并且只实例化一次,所以可能出现数据共享问题。
<html>
<head>
<title>object test</title>
<script language="javascript">
<!--
function Student(name, age) {
this.name = name;
this.age = age;
this.sayHello = function() {
alert("Hello, I am " + this.name);
}
this.toString = function() {
return this.name + " : " + this.age;
}
}
function CollegeStudent(major) {
this.major = major;
this.toString = function() {
return this.student.name + " : " + this.student.age + " : " + this.major;
}
}
var studentPrototype = new Student("siyuan", 24);
CollegeStudent.prototype.student = studentPrototype;
function testPrototype() {
var colleger1 = new CollegeStudent("software");
alert(colleger1);
var colleger2 = new CollegeStudent("hardware");
alert(colleger2);
colleger1.student.name = "siyuan1";
alert("modify colleger1.student.name");
alert(colleger1);
alert(colleger2);
}
-->
</script>
</head>
<body>
<input type="button" value="test prototype" onclick="javascript:testPrototype();"/><br>
</body>
</html>
分享到:
相关推荐
在 Ruby 中像 Javascript 一样进行原型面向编程。
本文实例讲述了javascript基于prototype实现类似OOP继承的方法。分享给大家供大家参考,具体如下: 这里要说明的是,公有属性(使用this.修饰符)可以被覆盖,私有属性(使用var 修饰符)不能被覆盖 子类不能访问父类...
originally intended it to be an extension to the Prototype JavaScript framework—MooTools has since grown into an independent, open-source, and very robust JavaScript framework with a solid team of ...
-目的说明它是在第1,2阶段文档演示的JavaScript面向对象的书写方式的进一步改进,它是现代JavaScript面向对象编程方式(使用基本类来编码)的过渡代码--没有它就没有当今的JS OOP--此人非常牛!!! 4. 最后使用IE...
npm install https://github.com/Perennials/prototype-js/tarball/master .containsEx() .indexOfEx() .indexOfEx() 功能。定义() 。延长() 。静止的() 。实施() .mixin() ResolveMixins() ....
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 ...
在梳理我的OOP并将其应用于JavaScript时,我遇到了在JS:原型中实例化对象的有趣区别。 我敢肯定,阅读本文的许多开发人员都在思考,仅学习此概念对于我来说是一个菜鸟,但是,嘿,我以前从未用过它。 所以我想学习...
类:分为公开的和私有的 代码如下: function Person(n){ var name=n; //私有属性 function hello(){} //私有方法一 var hello2() = function(){... //公开成员二 Person.prototype.SayHi = function(){} //公开方法二 v
class语法简介(面向对象的编程) 什么是this ? 它是函数的“所有者”。 大多数时候,它是函数的“调用方”。 另外,您可以使用以下方法进行设置: 箭头函数(箭头函数内部的... prototype . map = ( ) => { } A
目前网络上有关javascript实现OOP模式的方案基本上都是prototype模式,一般性的示例代码如下
百度百科中,这样描述了property:在JavaScript中,prototype对象是实现面向对象的一个重要机制。每个函数就是一个对象(Function),函数对象都有一个子对象 prototype对象,类是以函数的形式来定义的。prototype...
JavaScript面向对象编程指南不适合初学者 适合想提升JavaScript编程能力的人 prototype _proto_ constructor 浅拷贝深拷贝 JavaScript编程模式 设计模式
本实验的目的是使您熟悉用JavaScript编写和实现OOP的基础知识。 我们还将审查实验室中的测试。 每个动物实例应具有以下属性和方法: 名称 年龄 种类-例如马/狮子/猪 醒着-这应该默认为false oink()-返回“ oink...
上面的a-class提供了基于prototypejs的类实现的基本OOP功能。 安装 在命令行中: bower install a-class-above 来自bower.json文件 { "... "dependencies": { "a-class-above": "0.0.23" }, ... } 在你HTML...
这是JavaScript(JS)编程的存储库 :ledger: :scroll: :input_numbers: 我为我JavaScript全栈开发学习创建了此存储库。 :backpack: 前提条件和存储库 :books: : 良好的英语领域; 编程逻辑; 编程范例; HTML...
Javascript的继承和标准的oop继承有很大的区别,Javascript的继承是采用原型链的技术,每个类都会将“成员变量”和“成员函数”放到 prototype 上,Js++都过superclass将其链接起来,即 C.prototype.superclass = C....
二维的俄罗斯方块网页游戏,采用JavaScript编写,作者:高山流水。 游戏编写的几个要点: 1、网格的控制是写好该游戏的关键,比较重要的一点就是提前定义好方块集的map; 2、采用了OOP面向对象的编程思想,定义...
javascript中的OOP现在更方便了! (我希望)里面有什么? Object.prototype获得 2 个额外的东西: Parent :指向父原型的指针。 在Object : Parent = null的情况下,因为它是超类parent() :调用父方法的助手。 ...