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继承中方法属性的重写的更多相关文章

  1. js修改html中class属性

    document.getElementById("tr").setAttribute("class","styleclass"); 其中  ...

  2. java基础疑难点总结之成员变量的继承,方法重载与重写的区别,多态与动态绑定

    1.成员变量的继承 1.1要点 子类用extends关键字继承父类.子类中可以提供新的方法覆盖父类中的方法.子类中的方法不能直接访问父类中的私有域,子类可以用super关键字调用父类中的方法.在子类中 ...

  3. Js实现继承的方法

    原型的作用:1.将公共部分放入原型中,这样构造出的多个实例对象的公共部分只会占用一个公共空间,实现数据共享和节省内存空间 2.通过原型实现继承:构造函数模拟 "类"这个面向对象的概 ...

  4. JS 实现继承的方法 ES6 and ES5

    继承 ES6 方法  (类的继承) ES6中有一个属性的 extends 语法: ​ • class Father {} ​ • class Son extends Father{} ​ 注意:是子类 ...

  5. python__基础 : 多继承中方法的调用顺序 __mro__方法

    在多继承中,如果一个子类继承了两个平级的父类,而这两个父类有两个相同名字的方法,那么一般先继承谁,调用方法就调用先继承的那个父类的方法.如: class A: def test(self): prin ...

  6. C#继承中的override(重写)与new(覆盖)用法

    刚接触C#编程,我也是被override与new搞得晕头转向.于是花了点时间翻资料,看博客,终于算小有领悟,把学习笔记记录于此. 首先声明一个父类Animal类,与继承Animal的两个子类Dog类与 ...

  7. C++基础——类继承中方法重载

    一.前言 在上一篇C++基础博文中讨论了C++最基本的代码重用特性——类继承,派生类可以在继承基类元素的同时,添加新的成员和方法.但是没有考虑一种情况:派生类继承下来的方法的实现细节并不一定适合派生类 ...

  8. js 从一个对象中找到属性值相等的集合

    getobjs: function(objs, key, value) { var result = []; for (var i in objs) { var obj = $(objs[i]); i ...

  9. js 更改对象中的属性名

    const json = JSON.parse(JSON.stringify(options).replace(/name/g,"label")); 注: 1.options是需要 ...

随机推荐

  1. 互不侵犯_状压$dp$

    如果有想学习状压\(dp\)的童鞋,请光临博客状压\(dp\)初学 互不侵犯 题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八 ...

  2. SVN在vs2013中使用

    http://download.csdn.net/download/show_594/9112963 内包含VisualSVN 5.0.1的官方原版安装包及破解文件VisualSVN.Core.L.d ...

  3. 关于Fragment的setUserVisibleHint() 方法和onCreateView()的执行顺序

    1:setUserVisibleHint(boolean isVisibleToUser)的方法就很重要,根据方法名来看当前页面是否可见, 里面的boolean值就是判断当前页面是否可见的变量,所以大 ...

  4. grvphviz && dot脚本语言

    安装graphviz 可去官网下载http://www.graphviz.org/download/下载之后按步骤安装 打开编辑器,创建*.dot文件,编辑dot脚本代码,保存. D:\>dot ...

  5. Chromium Graphics: Compositor Thread Architecture

    Compositor Thread Architecture <jamesr, enne, vangelis, nduca> @chromium.org Goals The main re ...

  6. Ubuntu18.06 Mate桌面环境下VirtuslBox打开虚拟机“全局菜单”异常退出解决办法

    在安装完Ubuntu18.06 Mate桌面环境后在VirtuslBox里打开虚拟机会出现“全局菜单”异常退出问题. 产生上面问题的原因是你的虚拟机可能在 显示= >屏幕= >硬件加速里勾 ...

  7. js对浏览器产生的影响

     Js 是单线程执行引擎.在我们动态修改一些属性时会产生两种效果:    1.Repaint ----- 一部分重画,修改 div 的颜色呀,但是尺寸没有改变.    2.Reflow ---- 元素 ...

  8. VUE框架学习——脚手架的搭建

    #我的VUE框架学习 题记:初识VUE,觉得VUE十分的不错,故决定去深入的了解学习它,工欲善其事,必先利其器,下面是我搭建vue环境的过程! #一.项目搭建及初始化 1.安装:node.js:去官网 ...

  9. Delayer 基于 Redis 的延迟消息队列中间件

    Delayer 基于 Redis 的延迟消息队列中间件,采用 Golang 开发,支持 PHP.Golang 等多种语言客户端. 参考 有赞延迟队列设计 中的部分设计,优化后实现. 项目链接:http ...

  10. 【模板】2-SAT 问题(2-SAT)

    [模板]2-SAT 问题 题目背景 2-SAT 问题 模板 题目描述 有n个布尔变量 \(x_1\)​ ~ \(x_n\)​ ,另有m个需要满足的条件,每个条件的形式都是" \(x_i\) ...