如何扩展两个类

时间:2016-07-05 02:57:52

标签: javascript jquery selenium

如何在单个类中扩展两个类     e.g。

Class1- Common Class for all the other classes
Class2-(Where I use Class2 Extends Class1)
Class3-(Where I use Class3 Extends Class1)
Class4-(Where I use Class4 Extends Class1)

Suppose I introduce a ClassA whose methods are common for Class 2,3,4     
then how do I extend two classes
Class2-Class2 Extends Class1 Extends ClassA
Class3-Class3 Extends Class1 Extends ClassA
Class3-Class4 Extends Class1 Extends ClassA
Note that Class1 and ClassA have no methods in common.

2 个答案:

答案 0 :(得分:0)

基于对象的方法:

function foo() {}
function bar() {}
function bam() {}

var one = { foo: foo }; // Common Class for all the other classes
document.write(one.foo, '<br/>'); // 'function foo() {}'
document.write(one.bar, '<br/>'); // 'undefined'
document.write(one.bam, '<br/>'); // 'undefined'

var two = Object.create(one, {
  bar: { value: bar },
}); 
document.write(two.foo, '<br/>'); // 'function foo() {}'
document.write(two.bar, '<br/>'); // 'function bar() {}'
document.write(two.bam, '<br/>'); // 'undefined'

var three = Object.create(two, {
  bam: { value: bam },
});
document.write(three.foo, '<br/>'); // 'function foo() {}'
document.write(three.bar, '<br/>'); // 'function bar() {}'
document.write(three.bam, '<br/>'); // 'function bam() {}'

基于构造函数的方法:

function foo() {}
function bar() {}
function bam() {}

function One() {}
One.prototype.foo = foo;

var one = new One();
document.write(one.foo, '<br/>'); // 'function foo() {}'
document.write(one.bar, '<br/>'); // 'undefined'
document.write(one.bam, '<br/>'); // 'undefined'

function Two() {}
Two.prototype = new One();
Two.prototype.bar = bar // !

var two = new Two();
document.write(two.foo, '<br/>'); // 'function foo() {}'
document.write(two.bar, '<br/>'); // 'function bar() {}'
document.write(two.bam, '<br/>'); // 'undefined'

function Three() {}
Three.prototype = new Two();
Three.prototype.bam = bar // !

var three = new Three();
document.write(three.foo, '<br/>'); // 'function foo() {}'
document.write(three.bar, '<br/>'); // 'function bar() {}'
document.write(three.bam, '<br/>'); // 'function bam() {}'

基于类的方法(注意:JavaScript“类”只是语法糖):

function foo() {}
function bar() {}
function bam() {}

class One {
    foo() {}
}
var one = new One();
document.write(one.foo, '<br/>'); // 'function foo() {}'
document.write(one.bar, '<br/>'); // 'undefined'
document.write(one.bam, '<br/>'); // 'undefined'

class Two extends One {
    bar() {}
}
var two = new Two();
document.write(two.foo, '<br/>'); // 'function foo() {}'
document.write(two.bar, '<br/>'); // 'function bar() {}'
document.write(two.bam, '<br/>'); // 'undefined'

class Three extends Two {
    bam() {}
}
var three = new Three();
document.write(three.foo, '<br/>'); // 'function foo() {}'
document.write(three.bar, '<br/>'); // 'function bar() {}'
document.write(three.bam, '<br/>'); // 'function bam() {}'

基于混合的方法(注意这会将方法直接放在结果对象上):

var one = { foo: foo }; // Common Class for all the other classes
document.write(one.foo, '<br/>'); // 'function foo() {}'
document.write(one.bar, '<br/>'); // 'undefined'
document.write(one.bam, '<br/>'); // 'undefined'

var two = Object.assign({ bar: bar }, one);
document.write(two.foo, '<br/>'); // 'function foo() {}'
document.write(two.bar, '<br/>'); // 'function bar() {}'
document.write(two.bam, '<br/>'); // 'undefined'

var three = Object.assign({ bam: bam }, one, two);
document.write(three.foo, '<br/>'); // 'function foo() {}'
document.write(three.bar, '<br/>'); // 'function bar() {}'
document.write(three.bam, '<br/>'); // 'function bam() {}'

function foo() {}
function bar() {}
function bam() {}

答案 1 :(得分:0)

在Javascript中的示例中,我有四个类

ElementsCountInPage_Ex_5

假设我还有一个班级

Browser_Ex_2 Extends Declaration_Ex_1
Extends ElementsCountInPage_Ex_5

我想在剩下的三个类中调用第1和第5个公共类(例如Browser_Ex_2,Login_Ex_3和Summary_Ex_4),那么我如何在其他类中使用它的方法。

因为我们不能为两个类扩展两次,这将是不正确的不是它

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {

    var label = view as! UILabel!
    if label == nil {
        label = UILabel()
    }

    var data = pickerData[row]
    let title = NSAttributedString(string: data!, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(36.0, weight: UIFontWeightRegular)])
    label.attributedText = title
    label.textAlignment = .Center
    return label

}