js---17继承中方法属性的重写
function F(){};
var f = new F();
f.name = "cf";
f.hasOwnProperty("name");//true
F.prototype.age = 22;
f.hasOwnProperty("age");//false,age是原型对象的属性,name是F对象的属性,不是同一个。hasOwnProperty是对象的方法
F.prototype.isPrototypeOf(f);//true
//多态:编译时多态,运行时多态:方法重载、重写
//js不支持同名的方法
var o = {
run:function(){},
run:function(){}, //js同名会覆盖,run指向的是这个函数对象的地址,地址名加小括号就是这个类对象开始执行。function F(){} 既是类也是对象,new F()说明是一个类,F()函数名加小括号就是函数执行,说明是一个对象。
}
=======================================================================
function demo (a,b) {
console.log(demo.length);//形参个数,2
console.log(arguments.length);//实参个数,3
console.log(arguments[0]);
console.log(arguments[1]);
}
demo(4,5,6);
function add(){
var total = 0;
for (var i = arguments.length - 1; i >= 0; i--) {
total += arguments[i];
};
return total;
}
console.log(add(1));
console.log(add(1,2));//可变长度
function fontSize(){
var ele = document.getElementById("js");
if (arguments.length == 0){
return ele.style.fontSize;
}else{
ele.style.fontSize = arguments[0];
}
}
fontSize(18);
console.log(fontSize());
function setting(){
var ele = document.getElementById("js");
if (typeof arguments[0] ==="object"){
for(p in arguments[0]){//p是key,arguments[p]是value,
ele.style[p] = arguments[0][p];
}
}else{
ele.style.fontSize = arguments[0];
ele.style.backgroundColor= arguments[1]
; }
}
setting(18,"red");
setting({fontSize:20,backgroundColor:"green"});//js里面不能写同名的方法,所以只能够对方法的参数做判断,
==========================================================================
function demo(o){//demo是一个类,o是类的对象属性
o.run();//调用属性的方法
}
var o = {run:function(){
console.log("o is running...");
}};
demo(o);//类调用,java里面类不可以调用,这是跟java不一样的。
var p ={run:function(){
console.log("p is running...");
}};
demo(p);//函数是一个类也是一个对象,函数调用对象就会执行起来。
function F(){};
var f = new F();
F.prototype.run = function(){console.log("111");}//原型区域,对象. 可以访问
f.run();//
f.run = function(){console.log("222");};//只是给f自己加了一个方法,没有改变类的原型对象,相当于方法的重写。f.什么都是给自己对象加的
f.run();//
F.prototype.run();//
f.run = function(){//run指向一个对象的地址
console.log("222");
F.prototype.run();//重写父的,并且还要调用父的,
};
f.run();//222 , 111
=======================================================================
function Parent(){
this.run = function(){//现在把Parent当成类看,run是一个对象的地址,
console.log("parent is running...");
}
}
function Child(){
Parent.call(this);//继承父的方法,相当于父有了一个run方法,this.run = function(){console.log("parent is running...");},但是2个方法不是同一个,只是相当于把父的属性和方法在这里写了一遍。
var parentRun = this.run;//用this,parentRun指向run函数的地址,
this.run = function (){//run重新指向,重写,添加同名的子类方法
console.log("child is running...");
parentRun();//地址名小括号就是对象的执行
}
}
var c = new Child();//Child看成是类,c是对象
c.run();//run是函数的地址,地址小括号就是对象执行
========================================================================
function Parent(){
this.name = "333";//只能通过 对象.name 访问
var age = 34;//给嵌套函数使用
}
var p = new Parent();
console.log(p.name);//
console.log(Parent.name);//Parent
console.log(p.age);//undefined,
console.log(Parent.age);//undefined,
Parent.aa = "aa"; //静态属性,对象. 访问不到,类. 访问得到
Parent.prototype.aaa = "asa";//原型公有区域,对象. 访问得到,类. 访问不到
console.log(p.aa);//undefined,
console.log(Parent.aa);//aa
console.log(p.aaa);//asa,
console.log(Parent.aaa);//undefined
p.zz = "zz";//只是加给了对象自己,没有加给类和类的原型
console.log(p.zz);//zz
console.log(Parent.zz);//undefined
console.log(Parent.prototype.zz)//undefin
==========================================================================
function Parent(){
}
Parent.prototype.run = function() {
console.log("parent");
};
Child.prototype = Object.create(Parent.prototype);//继承
Child.prototype.constructor = Child;//修正
Child.super = Parent.prototype; //给Child增加静态属性
function Child(){
}
Child.prototype.run=function(){
console.log('child is running...');
Child.super.run();
}
var c = new Child();
c.run();
1.类里面的this.属性给对象用的,静态属性、方法给类用,什么都不加的和var给嵌套函数用,什么都不加的在window对象中。(静态属性、方法通过F.xx 添加)
2.对象的静态属性、方法给自己用。(静态属性、方法通过 p.xx 添加)
3.原型里面的属性、方法是给对象和原型自己用的。(通过 F.prototype.xx 添加)
<html>
<body>
<script type="text/javascript">
function F(){
this.name = "yw";
var age = 32;
sch = 890;
} var f = new F();
alert(f.name);//yw
alert(f.age);//undefined
alert(f.sch);//undefined
F.kr = "gh";
F.ss = function(){alert(123);}
alert(f.kr);//undefined
f.ss();//f.ss is not a function
alert(F.prototype.kr);//undefined
F.prototype.ss();//F.prototype.ss is not a function f.a = "a";
f.y = function(){alert("y");}
alert(F.a);//undefined
F.y();//F.y is not a function
alert(F.prototype.a);//undefined
F.prototype.y();//F.prototype.y is not a function F.prototype.o = "o";
F.prototype.oo = function(){alert("oo");}
alert(f.o);//o
f.oo();//oo
alert(F.o);//undefined
F.oo();//F.oo is not a function
</script>
</body> </html>
js---17继承中方法属性的重写的更多相关文章
- js修改html中class属性
document.getElementById("tr").setAttribute("class","styleclass"); 其中 ...
- java基础疑难点总结之成员变量的继承,方法重载与重写的区别,多态与动态绑定
1.成员变量的继承 1.1要点 子类用extends关键字继承父类.子类中可以提供新的方法覆盖父类中的方法.子类中的方法不能直接访问父类中的私有域,子类可以用super关键字调用父类中的方法.在子类中 ...
- Js实现继承的方法
原型的作用:1.将公共部分放入原型中,这样构造出的多个实例对象的公共部分只会占用一个公共空间,实现数据共享和节省内存空间 2.通过原型实现继承:构造函数模拟 "类"这个面向对象的概 ...
- JS 实现继承的方法 ES6 and ES5
继承 ES6 方法 (类的继承) ES6中有一个属性的 extends 语法: • class Father {} • class Son extends Father{} 注意:是子类 ...
- python__基础 : 多继承中方法的调用顺序 __mro__方法
在多继承中,如果一个子类继承了两个平级的父类,而这两个父类有两个相同名字的方法,那么一般先继承谁,调用方法就调用先继承的那个父类的方法.如: class A: def test(self): prin ...
- C#继承中的override(重写)与new(覆盖)用法
刚接触C#编程,我也是被override与new搞得晕头转向.于是花了点时间翻资料,看博客,终于算小有领悟,把学习笔记记录于此. 首先声明一个父类Animal类,与继承Animal的两个子类Dog类与 ...
- C++基础——类继承中方法重载
一.前言 在上一篇C++基础博文中讨论了C++最基本的代码重用特性——类继承,派生类可以在继承基类元素的同时,添加新的成员和方法.但是没有考虑一种情况:派生类继承下来的方法的实现细节并不一定适合派生类 ...
- js 从一个对象中找到属性值相等的集合
getobjs: function(objs, key, value) { var result = []; for (var i in objs) { var obj = $(objs[i]); i ...
- js 更改对象中的属性名
const json = JSON.parse(JSON.stringify(options).replace(/name/g,"label")); 注: 1.options是需要 ...
随机推荐
- [luogu P3360] 偷天换日 解题报告(树形DP)
题目链接:https://www.luogu.org/problemnew/show/P3360 题解: 首先我们把边上的消耗放到向下的点上,如果是叶子节点的话就先做一次0/1背包 发现这是一颗二叉树 ...
- linux下python3源码安装及卸载
Linux下Python3的源码编译安装和卸载方法 [日期:2019-06-21] 来源:博客园 作者:wuli潇萧 [字体:大 中 小] (一)Linux下软件的源码编译安装和卸载方法 L ...
- letCode(771 Jewels and Stones )
问题描述: You're given strings J representing the types of stones that are jewels, and S representing th ...
- POJ-1456 Supermarket 贪心问题 有时间限制的最小化惩罚问题
题目链接:https://cn.vjudge.net/problem/POJ-1456 此题与HDU-1789完全是一道题 题意 有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售 ...
- pip命令使用方法 与 错误处理
这里把学习到的pip命令写一个汇总,方便想不起来时使用 通过cmd输入pip可以显示提示信息,中文翻译如下: 1)显示某个包的信息 pip show selenium #显示selenium模块的信息 ...
- NET Core微服务之路:实战SkyWalking+Exceptionless体验生产下追踪系统
原文:NET Core微服务之路:实战SkyWalking+Exceptionless体验生产下追踪系统 前言 当一个APM或一个日志中心实际部署在生产环境中时,是有点力不从心的. 比如如下场景分析的 ...
- CodeForces 312B Archer
Archer Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...
- java流1----InputStream、OutputStream、Reader、Writer
字节流和字符流 顾名思义,字节流就是操作的是字节,字符流操作的就是字符.其中字节流又可以分为字节输入流(InputStream)和字节输出流(OutputStream).同样的字符流也可以分为字符输入 ...
- python的开发工具UliPad安装篇
之前文章里写过一个搭建windows下搭建Selenium+Eclipse+Python环境,如今认为这个Eclipse太大了,太笨重了,重新启动又慢,像Python脚本轻级语言,不是必需用那么大的工 ...
- [React] Implement a Higher Order Component with Render Props
When making a reusable component, you'll find that people often like to have the API they're most fa ...