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. CheckException和RuntimeException

    java文档中对RuntimeException的定义是: RuntimeException 是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类. 可能在执行方法期间抛出但未被捕获的 Runt ...

  2. Spring SpEL in JSP and Assign SpEL value to Java variable in JSP

    Spring SpEL in JSP and Assign SpEL value to Java variable in JSP method 1 use----ServletContextAttri ...

  3. Unified BeginFrame scheduling for Chrome

    Unified BeginFrame scheduling for Chrome http://goo.gl/D1Qxrr Status: http://crbug.com/401331 and ht ...

  4. 如何知道 CPU 是否支持虚拟化技术(VT)

    作者: Sk 译者: LCTT geekpi 我们已经知道如何检查你的 Linux 操作系统是 32 位还是 64 位以及如何知道你的 Linux 系统是物理机还是虚拟机.今天,我们将学习另一个有用的 ...

  5. ES6学习笔记(十三)Iterator遍历器和for...of循环

    1.概念 遍历器(Iterator)就是这样一种机制.它是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署 Iterator 接口,就可以完成遍历操作(即依次处理该数据结构的所有 ...

  6. ES6学习5 字符串的扩展

    1.ES6 为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历. for (let codePoint of 'foo') { console.log(codePoint) } // ...

  7. shell中处理用户输入

    1.使用命令行参数 在shell执行的时候命令行中输入的所有参数可以赋值给一些特殊变量,这些变量成为位置变量参数. 包括: $0返回脚本名称.$1为第一个参数.$2为第二个参数 ...$9第九个参数 ...

  8. LNMP安装部署开源IP管理工具phpipam

    1.数据库 mariadb 安装 //依赖安装 yum install -y apr* autoconf automake bison bzip2 bzip2* compat* \ cpp curl ...

  9. python 调试大法-大笨蛋的笔记

    说在前面 我觉得没有什么错误是调试器无法解决的,如果没有,那我再说一遍,如果有,那当我没说 一.抛出异常 可以通过 raise 语句抛出异常,使程序在我们已经知道的缺陷处停下,并进入到 except  ...

  10. ECNUOJ 2575 Separate Connections

    Separate Connections Time Limit:5000MS Memory Limit:65536KBTotal Submit:421 Accepted:41 Description  ...