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. [-] Failed to load plugin from /usr/share/metasploit-framework/plugins/db_autopwn: No classes were loaded from /usr/share/metasploit-framework/plugins/db_autopwn in the Msf::Plugin namespace.

    问题详情 然后,执行,出现如下问题,则说明大家的这个文件,下载不是完整的或者你上传不完整. msf > load db_autopwn [-] Failed to load plugin fro ...

  2. Android框架-Volley(一)

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...

  3. IHttpHandler的学习(0)

    本片文章转自网络 问题1:什么是HttpHandler?(Handler:处理者:那就是对Http请求的处理拉) 问题2:什么是HttpModule? 问题3:什么时候应该使用HttpHandler什 ...

  4. rpm卸载包遭遇 specifies multiple packages 错误

    使用 rpm删除软件时报错如下: [root@hostxxlidan]# rpm -qa |grep -i mysqlmysql-devel-5.0.95-5.el5_9mysql-devel-5.0 ...

  5. IBM主机上清除告警黄灯方法

    机器亮黄灯告警一般是有硬件问题(单电源等可能有安全隐患的硬件配置也可能造成黄灯亮起),见到后首先进行下硬件诊断,方法如下: 诊断系统,判断是否硬件故障:1.Root用户执行diag: 2.回车后,进第 ...

  6. UVA-11134 Fabled Rooks 贪心问题(区间贪心)

    题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...

  7. unity C# 获取有关文件、文件夹和驱动器的信息

    class FileSysInfo { static void Main() { // You can also use System.Environment.GetLogicalDrives to ...

  8. vue2.0-elementUI

    main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui' import ...

  9. vue10 v-text v-html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. BZOJ2016: [Usaco2010 Feb]Chocolate Eating

    [传送门:BZOJ2016] 简要题意: 贝西收到了N 块巧克力,她会在接下来的D 天里吃掉这些巧克力,她想制定一个计划,让她每 天的快乐度都保持在较高的水品上. 在第一天刚开始的时候,贝西的快乐度为 ...