javascript父子通信_javascript技巧
来源:动视网
责编:小采
时间:2020-11-27 20:37:14
javascript父子通信_javascript技巧
javascript父子通信_javascript技巧: var parent = { // say:function () { // alert(parent_say); }, alert:function () { // alert(parent); this.say(); } }; // parent.addChild(child, { // say:function () { // alert(child_say); }, alert:function () {
导读javascript父子通信_javascript技巧: var parent = { // say:function () { // alert(parent_say); }, alert:function () { // alert(parent); this.say(); } }; // parent.addChild(child, { // say:function () { // alert(child_say); }, alert:function () {

var parent =
{
//
say:function ()
{
//
alert("parent_say");
},
alert:function ()
{
//
alert("parent");
this.say();
}
};
//
parent.addChild("child",
{
//
say:function ()
{
//
alert("child_say");
},
alert:function ()
{
//
alert("child");
this.parent.say();
}
}
);
//parent.child.alert();
//parent.alert();
var c = parent.child;
c.alert();
script>
Object.prototype.addChild=function(oName, obj)
{
eval("var p=this."+oName+"= new Object()");
this[oName]=obj;
this[oName].parent=this;
}
script>
效果演示:
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
javascript父子通信_javascript技巧
javascript父子通信_javascript技巧: var parent = { // say:function () { // alert(parent_say); }, alert:function () { // alert(parent); this.say(); } }; // parent.addChild(child, { // say:function () { // alert(child_say); }, alert:function () {